~ubuntu-branches/ubuntu/utopic/gccgo-go/utopic

« back to all changes in this revision

Viewing changes to doc/progs/gobs2.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2014-01-27 09:18:55 UTC
  • Revision ID: package-import@ubuntu.com-20140127091855-zxfshmykfsyyw4b2
Tags: upstream-1.2
ImportĀ upstreamĀ versionĀ 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
}