go语言渐入佳境-网络[11]-gob

Gob是Go特有的序列化技术。它专门用于编码Go数据类型,目前不支持或由任何其他语言支持。它支持除通道和函数之外的所有Go数据类型。它支持所有类型和大小的整数,字符串和布尔值,结构,数组和切片。目前它在圆形结构(如环)方面存在一些问题,但随着时间的推移会有所改善。

Gob将类型信息编码为其序列化形式。这比X.509序列化中的类型信息要广泛得多,但远比XML文档中包含的类型信息更有效。

gob到文件中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* SaveGob
*/

package main

import (
"fmt"
"os"
"encoding/gob"
)

type Person struct {
Name Name
Email []Email
}

type Name struct {
Family string
Personal string
}

type Email struct {
Kind string
Address string
}

func main() {
person := Person{
Name: Name{Family: "Newmarch", Personal: "Jan"},
Email: []Email{Email{Kind: "home", Address: "jan@newmarch.name"},
Email{Kind: "work", Address: "j.newmarch@boxhill.edu.au"}}}

saveGob("person.gob", person)
}

func saveGob(fileName string, key interface{}) {
outFile, err := os.Create(fileName)
checkError(err)
encoder := gob.NewEncoder(outFile)
err = encoder.Encode(key)
checkError(err)
outFile.Close()
}

func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}

文件加载到内存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* LoadGob
*/

package main

import (
"fmt"
"os"
"encoding/gob"
)

type Person struct {
Name Name
Email []Email
}

type Name struct {
Family string
Personal string
}

type Email struct {
Kind string
Address string
}

func (p Person) String() string {
s := p.Name.Personal + " " + p.Name.Family
for _, v := range p.Email {
s += "\n" + v.Kind + ": " + v.Address
}
return s
}
func main() {
var person Person
loadGob("person.gob", &person)

fmt.Println("Person", person.String())
}

func loadGob(fileName string, key interface{}) {
inFile, err := os.Open(fileName)
checkError(err)
decoder := gob.NewDecoder(inFile)
err = decoder.Decode(key)
checkError(err)
inFile.Close()
}

func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}

gob服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* Gob EchoServer
*/
package main

import (
"fmt"
"net"
"os"
"encoding/gob"
)

type Person struct {
Name Name
Email []Email
}

type Name struct {
Family string
Personal string
}

type Email struct {
Kind string
Address string
}

func (p Person) String() string {
s := p.Name.Personal + " " + p.Name.Family
for _, v := range p.Email {
s += "\n" + v.Kind + ": " + v.Address
}
return s
}

func main() {

service := "0.0.0.0:1200"
tcpAddr, err := net.ResolveTCPAddr("tcp", service)
checkError(err)

listener, err := net.ListenTCP("tcp", tcpAddr)
checkError(err)

for {
conn, err := listener.Accept()
if err != nil {
continue
}

encoder := gob.NewEncoder(conn)
decoder := gob.NewDecoder(conn)

for n := 0; n < 10; n++ {
var person Person
decoder.Decode(&person)
fmt.Println(person.String())
encoder.Encode(person)
}
conn.Close() // we're finished
}
}

func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}

gob客户端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

/* Gob EchoClient
*/
package main

import (
"fmt"
"net"
"os"
"encoding/gob"
"bytes"
"io"
)

type Person struct {
Name Name
Email []Email
}

type Name struct {
Family string
Personal string
}

type Email struct {
Kind string
Address string
}

func (p Person) String() string {
s := p.Name.Personal + " " + p.Name.Family
for _, v := range p.Email {
s += "\n" + v.Kind + ": " + v.Address
}
return s
}

func main() {
person := Person{
Name: Name{Family: "Newmarch", Personal: "Jan"},
Email: []Email{Email{Kind: "home", Address: "jan@newmarch.name"},
Email{Kind: "work", Address: "j.newmarch@boxhill.edu.au"}}}

if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], "host:port")
os.Exit(1)
}
service := os.Args[1]

conn, err := net.Dial("tcp", service)
checkError(err)

encoder := gob.NewEncoder(conn)
decoder := gob.NewDecoder(conn)

for n := 0; n < 10; n++ {
encoder.Encode(person)
var newPerson Person
decoder.Decode(&newPerson)
fmt.Println(newPerson.String())
}

os.Exit(0)
}

func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}