├── README.md ├── etcd_register ├── etcd_register.exe └── main.go ├── options_pattern ├── options.go └── options_pattern.exe └── serialize ├── json ├── example1.go ├── json.exe └── json.txt ├── msgp ├── main.go ├── main_gen.go ├── main_gen_test.go ├── msg.txt └── msgp.exe ├── msgpack ├── main.go ├── msg.txt └── msgpack.exe └── protobuf ├── address └── person.pb.go ├── main.go ├── person.proto ├── proto.dat └── protobuf.exe /README.md: -------------------------------------------------------------------------------- 1 | # micro-kit 2 | -------------------------------------------------------------------------------- /etcd_register/etcd_register.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/etcd_register/etcd_register.exe -------------------------------------------------------------------------------- /etcd_register/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "log" 7 | "time" 8 | 9 | "github.com/coreos/etcd/clientv3" 10 | ) 11 | 12 | func main() { 13 | cli, err := clientv3.New(clientv3.Config{ 14 | Endpoints: []string{"127.0.0.1:2379"}, 15 | DialTimeout: time.Second, 16 | }) 17 | if err != nil { 18 | log.Fatal(err) 19 | } 20 | defer cli.Close() 21 | 22 | resp, err := cli.Grant(context.TODO(), 5) 23 | if err != nil { 24 | log.Fatal(err) 25 | } 26 | 27 | _, err = cli.Put(context.TODO(), "foo", "bar", clientv3.WithLease(resp.ID)) 28 | if err != nil { 29 | log.Fatal(err) 30 | } 31 | 32 | // the key 'foo' will be kept forever 33 | ch, kaerr := cli.KeepAlive(context.TODO(), resp.ID) 34 | if kaerr != nil { 35 | log.Fatal(kaerr) 36 | } 37 | 38 | for { 39 | ka := <-ch 40 | fmt.Println("ttl:", ka) 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /options_pattern/options.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | type Options struct { 6 | StrOption1 string 7 | StrOption2 string 8 | StrOption3 string 9 | IntOption1 int 10 | IntOption2 int 11 | IntOption3 int 12 | } 13 | 14 | func InitOptions1(strOption1 string, strOption2 string, strOption3 string, 15 | IntOption1 int, IntOption2 int, IntOption3 int) { 16 | options := &Options{} 17 | options.StrOption1 = strOption1 18 | options.StrOption2 = strOption2 19 | options.StrOption3 = strOption3 20 | options.IntOption1 = IntOption1 21 | options.IntOption2 = IntOption2 22 | options.IntOption3 = IntOption3 23 | 24 | fmt.Printf("init options1:%#v\n", options) 25 | return 26 | } 27 | 28 | func InitOptions2(opts ...interface{}) { 29 | options := &Options{} 30 | for index, opt := range opts { 31 | switch index { 32 | case 0: 33 | str, ok := opt.(string) 34 | if !ok { 35 | return 36 | } 37 | options.StrOption1 = str 38 | case 1: 39 | str, ok := opt.(string) 40 | if !ok { 41 | return 42 | } 43 | options.StrOption2 = str 44 | case 2: 45 | str, ok := opt.(string) 46 | if !ok { 47 | return 48 | } 49 | options.StrOption3 = str 50 | case 3: 51 | val, ok := opt.(int) 52 | if !ok { 53 | return 54 | } 55 | options.IntOption1 = val 56 | case 4: 57 | val, ok := opt.(int) 58 | if !ok { 59 | return 60 | } 61 | options.IntOption2 = val 62 | case 5: 63 | val, ok := opt.(int) 64 | if !ok { 65 | return 66 | } 67 | options.IntOption3 = val 68 | } 69 | } 70 | fmt.Printf("init options2:%#v\n", options) 71 | } 72 | 73 | type Option func(opts *Options) 74 | 75 | func InitOption3(opts ...Option) { 76 | options := &Options{} 77 | for _, opt := range opts { 78 | opt(options) 79 | } 80 | 81 | fmt.Printf("init options3:%#v\n", options) 82 | } 83 | 84 | func WithStringOption1(str string) Option { 85 | return func(opts *Options) { 86 | opts.StrOption1 = str 87 | } 88 | } 89 | 90 | func WithStringOption2(str string) Option { 91 | return func(opts *Options) { 92 | opts.StrOption2 = str 93 | } 94 | } 95 | 96 | func WithStringOption3(str string) Option { 97 | return func(opts *Options) { 98 | opts.StrOption3 = str 99 | } 100 | } 101 | 102 | func WithIntOption1(val int) Option { 103 | return func(opts *Options) { 104 | opts.IntOption1 = val 105 | } 106 | } 107 | 108 | func WithIntOption2(val int) Option { 109 | return func(opts *Options) { 110 | opts.IntOption2 = val 111 | } 112 | } 113 | 114 | func WithIntOption3(val int) Option { 115 | return func(opts *Options) { 116 | opts.IntOption3 = val 117 | } 118 | } 119 | 120 | func main() { 121 | InitOptions1("str1", "str2", "str3", 1, 2, 3) 122 | InitOptions2("str1", "str2", "str3", 1, 2, 3) 123 | InitOption3( 124 | WithStringOption1("str1"), 125 | WithStringOption3("str3"), 126 | WithIntOption3(3), 127 | WithIntOption2(2), 128 | WithIntOption1(1), 129 | WithStringOption2("str2"), 130 | ) 131 | } 132 | -------------------------------------------------------------------------------- /options_pattern/options_pattern.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/options_pattern/options_pattern.exe -------------------------------------------------------------------------------- /serialize/json/example1.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "encoding/json" 5 | "fmt" 6 | "io/ioutil" 7 | "time" 8 | ) 9 | 10 | type Birthday time.Time 11 | 12 | func (b *Birthday) MarshalJSON() (data []byte, err error) { 13 | 14 | //str :=fmt.Sprintf("2006/01/02", b) 15 | now := time.Time(*b) 16 | str := now.Format("2006-01-02") 17 | data, err = json.Marshal(str) 18 | fmt.Printf("data:%s\n", str) 19 | return 20 | } 21 | 22 | func (b *Birthday) UnmarshalJSON(data []byte) (err error) { 23 | 24 | str := string(data) 25 | now, _ := time.Parse("2006/01/02", str) 26 | *b = Birthday(now) 27 | return 28 | } 29 | 30 | type Person struct { 31 | Age int `json:"age"` 32 | Id int64 `json:"id,string"` 33 | Name string `json:"name_xx,omitempty"` 34 | Salary float32 `json:"-"` 35 | Birthday *Birthday `json:"birthday"` 36 | } 37 | 38 | func test1() { 39 | var p = &Person{ 40 | Age: 20, 41 | Id: 38888232322323222, 42 | Name: "axx", 43 | Salary: 38822.2, 44 | //Birthday: time.Now(), 45 | } 46 | 47 | var birthday Birthday 48 | birthday = Birthday(time.Now()) 49 | p.Birthday = &birthday 50 | 51 | data, err := json.Marshal(p) 52 | if err != nil { 53 | fmt.Printf("marshal failed, err:%v\n", err) 54 | return 55 | } 56 | 57 | ioutil.WriteFile("./json.txt", data, 0777) 58 | 59 | data2, err := ioutil.ReadFile("./json.txt") 60 | if err != nil { 61 | fmt.Printf("read file failed, err:%v\n", err) 62 | return 63 | 64 | } 65 | 66 | var person2 Person 67 | json.Unmarshal(data2, &person2) 68 | fmt.Printf("person2:%#v\n", person2) 69 | } 70 | 71 | func main() { 72 | 73 | test1() 74 | 75 | } 76 | -------------------------------------------------------------------------------- /serialize/json/json.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/json/json.exe -------------------------------------------------------------------------------- /serialize/json/json.txt: -------------------------------------------------------------------------------- 1 | {"age":20,"id":"38888232322323222","name_xx":"axx","birthday":"2019-05-26"} -------------------------------------------------------------------------------- /serialize/msgp/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | ) 7 | 8 | //go:generate msgp 9 | type Person struct { 10 | Age int `json:"age"` 11 | Id int64 `json:"id,string"` 12 | Name string `json:"name_xx,omitempty"` 13 | Salary float32 `json:"-"` 14 | } 15 | 16 | func test1() { 17 | 18 | var p = &Person{ 19 | Age: 20, 20 | Id: 38888232322323222, 21 | Name: "axx", 22 | Salary: 38822.2, 23 | } 24 | 25 | data, err := p.MarshalMsg(nil) 26 | if err != nil { 27 | fmt.Printf("marshal failed, err:%v\n", err) 28 | return 29 | } 30 | 31 | ioutil.WriteFile("./msg.txt", data, 0777) 32 | 33 | data2, err := ioutil.ReadFile("./msg.txt") 34 | if err != nil { 35 | fmt.Printf("read file failed, err:%v\n", err) 36 | return 37 | 38 | } 39 | 40 | var person2 Person 41 | person2.UnmarshalMsg(data2) 42 | fmt.Printf("person2:%#v\n", person2) 43 | 44 | } 45 | 46 | func main() { 47 | 48 | test1() 49 | 50 | } 51 | -------------------------------------------------------------------------------- /serialize/msgp/main_gen.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // Code generated by github.com/tinylib/msgp DO NOT EDIT. 4 | 5 | import ( 6 | "github.com/tinylib/msgp/msgp" 7 | ) 8 | 9 | // DecodeMsg implements msgp.Decodable 10 | func (z *Person) DecodeMsg(dc *msgp.Reader) (err error) { 11 | var field []byte 12 | _ = field 13 | var zb0001 uint32 14 | zb0001, err = dc.ReadMapHeader() 15 | if err != nil { 16 | err = msgp.WrapError(err) 17 | return 18 | } 19 | for zb0001 > 0 { 20 | zb0001-- 21 | field, err = dc.ReadMapKeyPtr() 22 | if err != nil { 23 | err = msgp.WrapError(err) 24 | return 25 | } 26 | switch msgp.UnsafeString(field) { 27 | case "Age": 28 | z.Age, err = dc.ReadInt() 29 | if err != nil { 30 | err = msgp.WrapError(err, "Age") 31 | return 32 | } 33 | case "Id": 34 | z.Id, err = dc.ReadInt64() 35 | if err != nil { 36 | err = msgp.WrapError(err, "Id") 37 | return 38 | } 39 | case "Name": 40 | z.Name, err = dc.ReadString() 41 | if err != nil { 42 | err = msgp.WrapError(err, "Name") 43 | return 44 | } 45 | case "Salary": 46 | z.Salary, err = dc.ReadFloat32() 47 | if err != nil { 48 | err = msgp.WrapError(err, "Salary") 49 | return 50 | } 51 | default: 52 | err = dc.Skip() 53 | if err != nil { 54 | err = msgp.WrapError(err) 55 | return 56 | } 57 | } 58 | } 59 | return 60 | } 61 | 62 | // EncodeMsg implements msgp.Encodable 63 | func (z *Person) EncodeMsg(en *msgp.Writer) (err error) { 64 | // map header, size 4 65 | // write "Age" 66 | err = en.Append(0x84, 0xa3, 0x41, 0x67, 0x65) 67 | if err != nil { 68 | return 69 | } 70 | err = en.WriteInt(z.Age) 71 | if err != nil { 72 | err = msgp.WrapError(err, "Age") 73 | return 74 | } 75 | // write "Id" 76 | err = en.Append(0xa2, 0x49, 0x64) 77 | if err != nil { 78 | return 79 | } 80 | err = en.WriteInt64(z.Id) 81 | if err != nil { 82 | err = msgp.WrapError(err, "Id") 83 | return 84 | } 85 | // write "Name" 86 | err = en.Append(0xa4, 0x4e, 0x61, 0x6d, 0x65) 87 | if err != nil { 88 | return 89 | } 90 | err = en.WriteString(z.Name) 91 | if err != nil { 92 | err = msgp.WrapError(err, "Name") 93 | return 94 | } 95 | // write "Salary" 96 | err = en.Append(0xa6, 0x53, 0x61, 0x6c, 0x61, 0x72, 0x79) 97 | if err != nil { 98 | return 99 | } 100 | err = en.WriteFloat32(z.Salary) 101 | if err != nil { 102 | err = msgp.WrapError(err, "Salary") 103 | return 104 | } 105 | return 106 | } 107 | 108 | // MarshalMsg implements msgp.Marshaler 109 | func (z *Person) MarshalMsg(b []byte) (o []byte, err error) { 110 | o = msgp.Require(b, z.Msgsize()) 111 | // map header, size 4 112 | // string "Age" 113 | o = append(o, 0x84, 0xa3, 0x41, 0x67, 0x65) 114 | o = msgp.AppendInt(o, z.Age) 115 | // string "Id" 116 | o = append(o, 0xa2, 0x49, 0x64) 117 | o = msgp.AppendInt64(o, z.Id) 118 | // string "Name" 119 | o = append(o, 0xa4, 0x4e, 0x61, 0x6d, 0x65) 120 | o = msgp.AppendString(o, z.Name) 121 | // string "Salary" 122 | o = append(o, 0xa6, 0x53, 0x61, 0x6c, 0x61, 0x72, 0x79) 123 | o = msgp.AppendFloat32(o, z.Salary) 124 | return 125 | } 126 | 127 | // UnmarshalMsg implements msgp.Unmarshaler 128 | func (z *Person) UnmarshalMsg(bts []byte) (o []byte, err error) { 129 | var field []byte 130 | _ = field 131 | var zb0001 uint32 132 | zb0001, bts, err = msgp.ReadMapHeaderBytes(bts) 133 | if err != nil { 134 | err = msgp.WrapError(err) 135 | return 136 | } 137 | for zb0001 > 0 { 138 | zb0001-- 139 | field, bts, err = msgp.ReadMapKeyZC(bts) 140 | if err != nil { 141 | err = msgp.WrapError(err) 142 | return 143 | } 144 | switch msgp.UnsafeString(field) { 145 | case "Age": 146 | z.Age, bts, err = msgp.ReadIntBytes(bts) 147 | if err != nil { 148 | err = msgp.WrapError(err, "Age") 149 | return 150 | } 151 | case "Id": 152 | z.Id, bts, err = msgp.ReadInt64Bytes(bts) 153 | if err != nil { 154 | err = msgp.WrapError(err, "Id") 155 | return 156 | } 157 | case "Name": 158 | z.Name, bts, err = msgp.ReadStringBytes(bts) 159 | if err != nil { 160 | err = msgp.WrapError(err, "Name") 161 | return 162 | } 163 | case "Salary": 164 | z.Salary, bts, err = msgp.ReadFloat32Bytes(bts) 165 | if err != nil { 166 | err = msgp.WrapError(err, "Salary") 167 | return 168 | } 169 | default: 170 | bts, err = msgp.Skip(bts) 171 | if err != nil { 172 | err = msgp.WrapError(err) 173 | return 174 | } 175 | } 176 | } 177 | o = bts 178 | return 179 | } 180 | 181 | // Msgsize returns an upper bound estimate of the number of bytes occupied by the serialized message 182 | func (z *Person) Msgsize() (s int) { 183 | s = 1 + 4 + msgp.IntSize + 3 + msgp.Int64Size + 5 + msgp.StringPrefixSize + len(z.Name) + 7 + msgp.Float32Size 184 | return 185 | } 186 | -------------------------------------------------------------------------------- /serialize/msgp/main_gen_test.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | // Code generated by github.com/tinylib/msgp DO NOT EDIT. 4 | 5 | import ( 6 | "bytes" 7 | "testing" 8 | 9 | "github.com/tinylib/msgp/msgp" 10 | ) 11 | 12 | func TestMarshalUnmarshalPerson(t *testing.T) { 13 | v := Person{} 14 | bts, err := v.MarshalMsg(nil) 15 | if err != nil { 16 | t.Fatal(err) 17 | } 18 | left, err := v.UnmarshalMsg(bts) 19 | if err != nil { 20 | t.Fatal(err) 21 | } 22 | if len(left) > 0 { 23 | t.Errorf("%d bytes left over after UnmarshalMsg(): %q", len(left), left) 24 | } 25 | 26 | left, err = msgp.Skip(bts) 27 | if err != nil { 28 | t.Fatal(err) 29 | } 30 | if len(left) > 0 { 31 | t.Errorf("%d bytes left over after Skip(): %q", len(left), left) 32 | } 33 | } 34 | 35 | func BenchmarkMarshalMsgPerson(b *testing.B) { 36 | v := Person{} 37 | b.ReportAllocs() 38 | b.ResetTimer() 39 | for i := 0; i < b.N; i++ { 40 | v.MarshalMsg(nil) 41 | } 42 | } 43 | 44 | func BenchmarkAppendMsgPerson(b *testing.B) { 45 | v := Person{} 46 | bts := make([]byte, 0, v.Msgsize()) 47 | bts, _ = v.MarshalMsg(bts[0:0]) 48 | b.SetBytes(int64(len(bts))) 49 | b.ReportAllocs() 50 | b.ResetTimer() 51 | for i := 0; i < b.N; i++ { 52 | bts, _ = v.MarshalMsg(bts[0:0]) 53 | } 54 | } 55 | 56 | func BenchmarkUnmarshalPerson(b *testing.B) { 57 | v := Person{} 58 | bts, _ := v.MarshalMsg(nil) 59 | b.ReportAllocs() 60 | b.SetBytes(int64(len(bts))) 61 | b.ResetTimer() 62 | for i := 0; i < b.N; i++ { 63 | _, err := v.UnmarshalMsg(bts) 64 | if err != nil { 65 | b.Fatal(err) 66 | } 67 | } 68 | } 69 | 70 | func TestEncodeDecodePerson(t *testing.T) { 71 | v := Person{} 72 | var buf bytes.Buffer 73 | msgp.Encode(&buf, &v) 74 | 75 | m := v.Msgsize() 76 | if buf.Len() > m { 77 | t.Logf("WARNING: Msgsize() for %v is inaccurate", v) 78 | } 79 | 80 | vn := Person{} 81 | err := msgp.Decode(&buf, &vn) 82 | if err != nil { 83 | t.Error(err) 84 | } 85 | 86 | buf.Reset() 87 | msgp.Encode(&buf, &v) 88 | err = msgp.NewReader(&buf).Skip() 89 | if err != nil { 90 | t.Error(err) 91 | } 92 | } 93 | 94 | func BenchmarkEncodePerson(b *testing.B) { 95 | v := Person{} 96 | var buf bytes.Buffer 97 | msgp.Encode(&buf, &v) 98 | b.SetBytes(int64(buf.Len())) 99 | en := msgp.NewWriter(msgp.Nowhere) 100 | b.ReportAllocs() 101 | b.ResetTimer() 102 | for i := 0; i < b.N; i++ { 103 | v.EncodeMsg(en) 104 | } 105 | en.Flush() 106 | } 107 | 108 | func BenchmarkDecodePerson(b *testing.B) { 109 | v := Person{} 110 | var buf bytes.Buffer 111 | msgp.Encode(&buf, &v) 112 | b.SetBytes(int64(buf.Len())) 113 | rd := msgp.NewEndlessReader(buf.Bytes(), b) 114 | dc := msgp.NewReader(rd) 115 | b.ReportAllocs() 116 | b.ResetTimer() 117 | for i := 0; i < b.N; i++ { 118 | err := v.DecodeMsg(dc) 119 | if err != nil { 120 | b.Fatal(err) 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /serialize/msgp/msg.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/msgp/msg.txt -------------------------------------------------------------------------------- /serialize/msgp/msgp.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/msgp/msgp.exe -------------------------------------------------------------------------------- /serialize/msgpack/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | 7 | "github.com/vmihailenco/msgpack" 8 | ) 9 | 10 | type Person struct { 11 | Age int `json:"age"` 12 | Id int64 `json:"id,string"` 13 | Name string `json:"name_xx,omitempty"` 14 | Salary float32 `json:"-"` 15 | } 16 | 17 | func test1() { 18 | var p = &Person{ 19 | Age: 20, 20 | Id: 38888232322323222, 21 | Name: "axx", 22 | Salary: 38822.2, 23 | } 24 | 25 | data, err := msgpack.Marshal(p) 26 | if err != nil { 27 | fmt.Printf("marshal failed, err:%v\n", err) 28 | return 29 | } 30 | 31 | ioutil.WriteFile("./msg.txt", data, 0777) 32 | 33 | data2, err := ioutil.ReadFile("./msg.txt") 34 | if err != nil { 35 | fmt.Printf("read file failed, err:%v\n", err) 36 | return 37 | 38 | } 39 | 40 | var person2 Person 41 | msgpack.Unmarshal(data2, &person2) 42 | fmt.Printf("person2:%#v\n", person2) 43 | } 44 | 45 | func main() { 46 | 47 | test1() 48 | 49 | } 50 | -------------------------------------------------------------------------------- /serialize/msgpack/msg.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/msgpack/msg.txt -------------------------------------------------------------------------------- /serialize/msgpack/msgpack.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/msgpack/msgpack.exe -------------------------------------------------------------------------------- /serialize/protobuf/address/person.pb.go: -------------------------------------------------------------------------------- 1 | // Code generated by protoc-gen-go. DO NOT EDIT. 2 | // source: person.proto 3 | 4 | package address 5 | 6 | /* 7 | 包名,通过protoc生成时go文件时 8 | */ 9 | 10 | import proto "github.com/golang/protobuf/proto" 11 | import fmt "fmt" 12 | import math "math" 13 | 14 | // Reference imports to suppress errors if they are not otherwise used. 15 | var _ = proto.Marshal 16 | var _ = fmt.Errorf 17 | var _ = math.Inf 18 | 19 | // This is a compile-time assertion to ensure that this generated file 20 | // is compatible with the proto package it is being compiled against. 21 | // A compilation error at this line likely means your copy of the 22 | // proto package needs to be updated. 23 | const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package 24 | 25 | // 手机类型 26 | // 枚举类型第一个字段必须为0 27 | type PhoneType int32 28 | 29 | const ( 30 | PhoneType_HOME PhoneType = 0 31 | PhoneType_WORK PhoneType = 1 32 | ) 33 | 34 | var PhoneType_name = map[int32]string{ 35 | 0: "HOME", 36 | 1: "WORK", 37 | } 38 | var PhoneType_value = map[string]int32{ 39 | "HOME": 0, 40 | "WORK": 1, 41 | } 42 | 43 | func (x PhoneType) String() string { 44 | return proto.EnumName(PhoneType_name, int32(x)) 45 | } 46 | func (PhoneType) EnumDescriptor() ([]byte, []int) { 47 | return fileDescriptor_person_5fdff31bc2e297f6, []int{0} 48 | } 49 | 50 | // 手机 51 | type Phone struct { 52 | Type PhoneType `protobuf:"varint,1,opt,name=type,proto3,enum=address.PhoneType" json:"type,omitempty"` 53 | Number string `protobuf:"bytes,2,opt,name=number,proto3" json:"number,omitempty"` 54 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 55 | XXX_unrecognized []byte `json:"-"` 56 | XXX_sizecache int32 `json:"-"` 57 | } 58 | 59 | func (m *Phone) Reset() { *m = Phone{} } 60 | func (m *Phone) String() string { return proto.CompactTextString(m) } 61 | func (*Phone) ProtoMessage() {} 62 | func (*Phone) Descriptor() ([]byte, []int) { 63 | return fileDescriptor_person_5fdff31bc2e297f6, []int{0} 64 | } 65 | func (m *Phone) XXX_Unmarshal(b []byte) error { 66 | return xxx_messageInfo_Phone.Unmarshal(m, b) 67 | } 68 | func (m *Phone) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 69 | return xxx_messageInfo_Phone.Marshal(b, m, deterministic) 70 | } 71 | func (dst *Phone) XXX_Merge(src proto.Message) { 72 | xxx_messageInfo_Phone.Merge(dst, src) 73 | } 74 | func (m *Phone) XXX_Size() int { 75 | return xxx_messageInfo_Phone.Size(m) 76 | } 77 | func (m *Phone) XXX_DiscardUnknown() { 78 | xxx_messageInfo_Phone.DiscardUnknown(m) 79 | } 80 | 81 | var xxx_messageInfo_Phone proto.InternalMessageInfo 82 | 83 | func (m *Phone) GetType() PhoneType { 84 | if m != nil { 85 | return m.Type 86 | } 87 | return PhoneType_HOME 88 | } 89 | 90 | func (m *Phone) GetNumber() string { 91 | if m != nil { 92 | return m.Number 93 | } 94 | return "" 95 | } 96 | 97 | // 人 98 | type Person struct { 99 | // 后面的数字表示标识号 100 | Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` 101 | Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` 102 | // repeated表示可重复 103 | // 可以有多个手机 104 | Phones []*Phone `protobuf:"bytes,3,rep,name=phones,proto3" json:"phones,omitempty"` 105 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 106 | XXX_unrecognized []byte `json:"-"` 107 | XXX_sizecache int32 `json:"-"` 108 | } 109 | 110 | func (m *Person) Reset() { *m = Person{} } 111 | func (m *Person) String() string { return proto.CompactTextString(m) } 112 | func (*Person) ProtoMessage() {} 113 | func (*Person) Descriptor() ([]byte, []int) { 114 | return fileDescriptor_person_5fdff31bc2e297f6, []int{1} 115 | } 116 | func (m *Person) XXX_Unmarshal(b []byte) error { 117 | return xxx_messageInfo_Person.Unmarshal(m, b) 118 | } 119 | func (m *Person) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 120 | return xxx_messageInfo_Person.Marshal(b, m, deterministic) 121 | } 122 | func (dst *Person) XXX_Merge(src proto.Message) { 123 | xxx_messageInfo_Person.Merge(dst, src) 124 | } 125 | func (m *Person) XXX_Size() int { 126 | return xxx_messageInfo_Person.Size(m) 127 | } 128 | func (m *Person) XXX_DiscardUnknown() { 129 | xxx_messageInfo_Person.DiscardUnknown(m) 130 | } 131 | 132 | var xxx_messageInfo_Person proto.InternalMessageInfo 133 | 134 | func (m *Person) GetId() int32 { 135 | if m != nil { 136 | return m.Id 137 | } 138 | return 0 139 | } 140 | 141 | func (m *Person) GetName() string { 142 | if m != nil { 143 | return m.Name 144 | } 145 | return "" 146 | } 147 | 148 | func (m *Person) GetPhones() []*Phone { 149 | if m != nil { 150 | return m.Phones 151 | } 152 | return nil 153 | } 154 | 155 | // 联系簿 156 | type ContactBook struct { 157 | Persons []*Person `protobuf:"bytes,1,rep,name=persons,proto3" json:"persons,omitempty"` 158 | XXX_NoUnkeyedLiteral struct{} `json:"-"` 159 | XXX_unrecognized []byte `json:"-"` 160 | XXX_sizecache int32 `json:"-"` 161 | } 162 | 163 | func (m *ContactBook) Reset() { *m = ContactBook{} } 164 | func (m *ContactBook) String() string { return proto.CompactTextString(m) } 165 | func (*ContactBook) ProtoMessage() {} 166 | func (*ContactBook) Descriptor() ([]byte, []int) { 167 | return fileDescriptor_person_5fdff31bc2e297f6, []int{2} 168 | } 169 | func (m *ContactBook) XXX_Unmarshal(b []byte) error { 170 | return xxx_messageInfo_ContactBook.Unmarshal(m, b) 171 | } 172 | func (m *ContactBook) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { 173 | return xxx_messageInfo_ContactBook.Marshal(b, m, deterministic) 174 | } 175 | func (dst *ContactBook) XXX_Merge(src proto.Message) { 176 | xxx_messageInfo_ContactBook.Merge(dst, src) 177 | } 178 | func (m *ContactBook) XXX_Size() int { 179 | return xxx_messageInfo_ContactBook.Size(m) 180 | } 181 | func (m *ContactBook) XXX_DiscardUnknown() { 182 | xxx_messageInfo_ContactBook.DiscardUnknown(m) 183 | } 184 | 185 | var xxx_messageInfo_ContactBook proto.InternalMessageInfo 186 | 187 | func (m *ContactBook) GetPersons() []*Person { 188 | if m != nil { 189 | return m.Persons 190 | } 191 | return nil 192 | } 193 | 194 | func init() { 195 | proto.RegisterType((*Phone)(nil), "address.Phone") 196 | proto.RegisterType((*Person)(nil), "address.Person") 197 | proto.RegisterType((*ContactBook)(nil), "address.ContactBook") 198 | proto.RegisterEnum("address.PhoneType", PhoneType_name, PhoneType_value) 199 | } 200 | 201 | func init() { proto.RegisterFile("person.proto", fileDescriptor_person_5fdff31bc2e297f6) } 202 | 203 | var fileDescriptor_person_5fdff31bc2e297f6 = []byte{ 204 | // 220 bytes of a gzipped FileDescriptorProto 205 | 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0xe2, 0x29, 0x48, 0x2d, 0x2a, 206 | 0xce, 0xcf, 0xd3, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62, 0x4f, 0x4c, 0x49, 0x29, 0x4a, 0x2d, 207 | 0x2e, 0x56, 0x72, 0xe7, 0x62, 0x0d, 0xc8, 0xc8, 0xcf, 0x4b, 0x15, 0x52, 0xe3, 0x62, 0x29, 0xa9, 208 | 0x2c, 0x48, 0x95, 0x60, 0x54, 0x60, 0xd4, 0xe0, 0x33, 0x12, 0xd2, 0x83, 0x2a, 0xd0, 0x03, 0xcb, 209 | 0x86, 0x54, 0x16, 0xa4, 0x06, 0x81, 0xe5, 0x85, 0xc4, 0xb8, 0xd8, 0xf2, 0x4a, 0x73, 0x93, 0x52, 210 | 0x8b, 0x24, 0x98, 0x14, 0x18, 0x35, 0x38, 0x83, 0xa0, 0x3c, 0xa5, 0x10, 0x2e, 0xb6, 0x00, 0xb0, 211 | 0x0d, 0x42, 0x7c, 0x5c, 0x4c, 0x99, 0x29, 0x60, 0x73, 0x58, 0x83, 0x98, 0x32, 0x53, 0x84, 0x84, 212 | 0xb8, 0x58, 0xf2, 0x12, 0x73, 0x53, 0xa1, 0xea, 0xc1, 0x6c, 0x21, 0x35, 0x2e, 0xb6, 0x02, 0x90, 213 | 0xc1, 0xc5, 0x12, 0xcc, 0x0a, 0xcc, 0x1a, 0xdc, 0x46, 0x7c, 0xa8, 0xf6, 0x05, 0x41, 0x65, 0x95, 214 | 0x2c, 0xb8, 0xb8, 0x9d, 0xf3, 0xf3, 0x4a, 0x12, 0x93, 0x4b, 0x9c, 0xf2, 0xf3, 0xb3, 0x85, 0x34, 215 | 0xb9, 0xd8, 0x21, 0xde, 0x28, 0x96, 0x60, 0x04, 0xeb, 0xe3, 0x47, 0xe8, 0x03, 0x8b, 0x07, 0xc1, 216 | 0xe4, 0xb5, 0xe4, 0xb9, 0x38, 0xe1, 0x4e, 0x17, 0xe2, 0xe0, 0x62, 0xf1, 0xf0, 0xf7, 0x75, 0x15, 217 | 0x60, 0x00, 0xb1, 0xc2, 0xfd, 0x83, 0xbc, 0x05, 0x18, 0x93, 0xd8, 0xc0, 0x21, 0x61, 0x0c, 0x08, 218 | 0x00, 0x00, 0xff, 0xff, 0xc7, 0xa8, 0x7f, 0x9e, 0x19, 0x01, 0x00, 0x00, 219 | } 220 | -------------------------------------------------------------------------------- /serialize/protobuf/main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "io/ioutil" 6 | 7 | "github.com/golang/protobuf/proto" 8 | "github.com/ibinarytree/micro-kit/serialize/protobuf/address" 9 | ) 10 | 11 | func main() { 12 | var person address.Person 13 | person.Id = 3988222 14 | person.Name = "hua" 15 | 16 | var phone address.Phone 17 | phone.Number = "13872832832" 18 | person.Phones = append(person.Phones, &phone) 19 | 20 | data, err := proto.Marshal(&person) 21 | if err != nil { 22 | fmt.Printf("marshal failed, err:%v\n", err) 23 | return 24 | } 25 | 26 | ioutil.WriteFile("./proto.dat", data, 0777) 27 | 28 | data2, err := ioutil.ReadFile("./proto.dat") 29 | if err != nil { 30 | fmt.Printf("read file failed, err:%v\n", err) 31 | return 32 | } 33 | 34 | var person2 address.Person 35 | proto.Unmarshal(data2, &person2) 36 | 37 | fmt.Printf("unmarshal person:%#v\n", person2) 38 | } 39 | -------------------------------------------------------------------------------- /serialize/protobuf/person.proto: -------------------------------------------------------------------------------- 1 | //指定版本 2 | //注意proto3与proto2的写法有些不同 3 | syntax = "proto3"; 4 | 5 | //包名,通过protoc生成时go文件时 6 | package address; 7 | 8 | //手机类型 9 | //枚举类型第一个字段必须为0 10 | enum PhoneType { 11 | HOME = 0; 12 | WORK = 1; 13 | } 14 | 15 | //手机 16 | message Phone { 17 | PhoneType type = 1; 18 | string number = 2; 19 | } 20 | 21 | //人 22 | message Person { 23 | //后面的数字表示标识号 24 | int32 id = 1; 25 | string name = 2; 26 | //repeated表示可重复 27 | //可以有多个手机 28 | repeated Phone phones = 3; 29 | } 30 | 31 | //联系簿 32 | message ContactBook { 33 | repeated Person persons = 1; 34 | } -------------------------------------------------------------------------------- /serialize/protobuf/proto.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/protobuf/proto.dat -------------------------------------------------------------------------------- /serialize/protobuf/protobuf.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ibinarytree/micro-kit/d1323c2426bcbdcf192f582176c5bb8bb0ae3696/serialize/protobuf/protobuf.exe --------------------------------------------------------------------------------