github.com/roboticscm/goman@v0.0.0-20210203095141-87c07b4a0a55/doc/progs/gobs2.go (about) 1 // compile 2 3 // Copyright 2011 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package main 8 9 import ( 10 "bytes" 11 "encoding/gob" 12 "fmt" 13 "log" 14 ) 15 16 type P struct { 17 X, Y, Z int 18 Name string 19 } 20 21 type Q struct { 22 X, Y *int32 23 Name string 24 } 25 26 func main() { 27 // Initialize the encoder and decoder. Normally enc and dec would be 28 // bound to network connections and the encoder and decoder would 29 // run in different processes. 30 var network bytes.Buffer // Stand-in for a network connection 31 enc := gob.NewEncoder(&network) // Will write to network. 32 dec := gob.NewDecoder(&network) // Will read from network. 33 // Encode (send) the value. 34 err := enc.Encode(P{3, 4, 5, "Pythagoras"}) 35 if err != nil { 36 log.Fatal("encode error:", err) 37 } 38 // Decode (receive) the value. 39 var q Q 40 err = dec.Decode(&q) 41 if err != nil { 42 log.Fatal("decode error:", err) 43 } 44 fmt.Printf("%q: {%d,%d}\n", q.Name, *q.X, *q.Y) 45 }