1 // Copyright 2017 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
17 func Example_binaryMarshaler() {
19 input1
= "The tunneling gopher digs downwards, "
20 input2
= "unaware of what he will find."
24 first
.Write([]byte(input1
))
26 marshaler
, ok
:= first
.(encoding
.BinaryMarshaler
)
28 log
.Fatal("first does not implement encoding.BinaryMarshaler")
30 state
, err
:= marshaler
.MarshalBinary()
32 log
.Fatal("unable to marshal hash:", err
)
35 second
:= sha256
.New()
37 unmarshaler
, ok
:= second
.(encoding
.BinaryUnmarshaler
)
39 log
.Fatal("second does not implement encoding.BinaryUnmarshaler")
41 if err
:= unmarshaler
.UnmarshalBinary(state
); err
!= nil {
42 log
.Fatal("unable to unmarshal hash:", err
)
45 first
.Write([]byte(input2
))
46 second
.Write([]byte(input2
))
48 fmt
.Printf("%x\n", first
.Sum(nil))
49 fmt
.Println(bytes
.Equal(first
.Sum(nil), second
.Sum(nil)))
51 // 57d51a066f3a39942649cd9a76c77e97ceab246756ff3888659e6aa5a07f4a52