github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/encoding/gob/example_encdec_test.go (about) 1 // Copyright 2013 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. 4 5 package gob_test 6 7 import ( 8 "github.com/shogo82148/std/bytes" 9 "github.com/shogo82148/std/encoding/gob" 10 "github.com/shogo82148/std/fmt" 11 "github.com/shogo82148/std/log" 12 ) 13 14 // この例では、カスタムのエンコーディングとデコーディングメソッドを実装した値を伝送します。 15 func Example_encodeDecode() { 16 var network bytes.Buffer // ネットワークの代わり。 17 18 // エンコーダを作成し、値を送信します。 19 enc := gob.NewEncoder(&network) 20 err := enc.Encode(Vector{3, 4, 5}) 21 if err != nil { 22 log.Fatal("encode:", err) 23 } 24 25 // デコーダを作成し、値を受信します。 26 dec := gob.NewDecoder(&network) 27 var v Vector 28 err = dec.Decode(&v) 29 if err != nil { 30 log.Fatal("decode:", err) 31 } 32 fmt.Println(v) 33 34 // Output: 35 // {3 4 5} 36 }