PR c++/86342 - -Wdeprecated-copy and system headers.
[official-gcc.git] / libgo / go / hash / example_test.go
blob49dc8fc8abb7df5bc08e619f9d1e4e0ceb74b596
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.
5 // +build ignore
7 package hash_test
9 import (
10 "bytes"
11 "crypto/sha256"
12 "encoding"
13 "fmt"
14 "log"
17 func Example_binaryMarshaler() {
18 const (
19 input1 = "The tunneling gopher digs downwards, "
20 input2 = "unaware of what he will find."
23 first := sha256.New()
24 first.Write([]byte(input1))
26 marshaler, ok := first.(encoding.BinaryMarshaler)
27 if !ok {
28 log.Fatal("first does not implement encoding.BinaryMarshaler")
30 state, err := marshaler.MarshalBinary()
31 if err != nil {
32 log.Fatal("unable to marshal hash:", err)
35 second := sha256.New()
37 unmarshaler, ok := second.(encoding.BinaryUnmarshaler)
38 if !ok {
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)))
50 // Output:
51 // 57d51a066f3a39942649cd9a76c77e97ceab246756ff3888659e6aa5a07f4a52
52 // true