diff --git a/go.mod b/go.mod index 6ae552b..ceaceeb 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,10 @@ require github.com/fogleman/gg v1.3.0 require github.com/hajimehoshi/ebiten/v2 v2.2.5 -require golang.org/x/term v0.11.0 +require ( + golang.org/x/term v0.11.0 + google.golang.org/protobuf v1.31.0 +) require ( github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be // indirect diff --git a/go.sum b/go.sum index 1f87426..b1b84b7 100644 --- a/go.sum +++ b/go.sum @@ -5,6 +5,9 @@ github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be h1:vEIVIuBApE github.com/go-gl/glfw/v3.3/glfw v0.0.0-20210727001814-0db043d8d5be/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g= github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k= +github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= +github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU= +github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/hajimehoshi/bitmapfont/v2 v2.1.3/go.mod h1:2BnYrkTQGThpr/CY6LorYtt/zEPNzvE/ND69CRTaHMs= github.com/hajimehoshi/ebiten/v2 v2.2.5 h1:i6NdS6pEi5kgfTh+4XAVCVtCXxjTyxzU1cj1oqHWkZQ= github.com/hajimehoshi/ebiten/v2 v2.2.5/go.mod h1:olKl/qqhMBBAm2oI7Zy292nCtE+nitlmYKNF3UpbFn0= @@ -96,4 +99,9 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= +google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8= +google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= diff --git a/offscript/protobufs/client/main.go b/offscript/protobufs/client/main.go new file mode 100644 index 0000000..588f9bf --- /dev/null +++ b/offscript/protobufs/client/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "io" + "net/http" + + "github.com/samiam2013/learnGo/offscript/protobufs/protobuf" + "google.golang.org/protobuf/proto" +) + +func main() { + // start an http client + client := &http.Client{} + req, err := http.NewRequest("GET", "http://localhost:8080", nil) + if err != nil { + panic(err) + } + // set the content type to application/x-protobuf + req.Header.Set("Content-Type", "application/x-protobuf") + // make the request + resp, err := client.Do(req) + if err != nil { + panic(err) + } + defer resp.Body.Close() + // read the response + hotel := &protobuf.Hotel{} + b, err := io.ReadAll(resp.Body) + if err != nil { + panic(err) + } + if err := proto.Unmarshal(b, hotel); err != nil { + panic(err) + } + fmt.Printf("%+v\n", hotel) + +} diff --git a/offscript/protobufs/hotel.proto b/offscript/protobufs/hotel.proto new file mode 100644 index 0000000..2668efc --- /dev/null +++ b/offscript/protobufs/hotel.proto @@ -0,0 +1,12 @@ +syntax = "proto3"; +package protoproto; + +option go_package = "./protobuf"; + +message hotel { + int32 id = 1; + string name = 2; + string address = 3; + float lattitude = 4; + float longitude = 5; +} \ No newline at end of file diff --git a/offscript/protobufs/proto/hotel.pb.go b/offscript/protobufs/proto/hotel.pb.go new file mode 100644 index 0000000..d55b954 --- /dev/null +++ b/offscript/protobufs/proto/hotel.pb.go @@ -0,0 +1,179 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v4.25.1 +// source: hotel.proto + +package proto + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Hotel struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + Address string `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty"` + Lattitude float32 `protobuf:"fixed32,4,opt,name=lattitude,proto3" json:"lattitude,omitempty"` + Longitude float32 `protobuf:"fixed32,5,opt,name=longitude,proto3" json:"longitude,omitempty"` +} + +func (x *Hotel) Reset() { + *x = Hotel{} + if protoimpl.UnsafeEnabled { + mi := &file_hotel_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Hotel) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Hotel) ProtoMessage() {} + +func (x *Hotel) ProtoReflect() protoreflect.Message { + mi := &file_hotel_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Hotel.ProtoReflect.Descriptor instead. +func (*Hotel) Descriptor() ([]byte, []int) { + return file_hotel_proto_rawDescGZIP(), []int{0} +} + +func (x *Hotel) GetId() int32 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Hotel) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Hotel) GetAddress() string { + if x != nil { + return x.Address + } + return "" +} + +func (x *Hotel) GetLattitude() float32 { + if x != nil { + return x.Lattitude + } + return 0 +} + +func (x *Hotel) GetLongitude() float32 { + if x != nil { + return x.Longitude + } + return 0 +} + +var File_hotel_proto protoreflect.FileDescriptor + +var file_hotel_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x68, 0x6f, 0x74, 0x65, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x81, 0x01, 0x0a, 0x05, 0x68, 0x6f, + 0x74, 0x65, 0x6c, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, + 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, + 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x61, 0x74, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x02, 0x52, 0x09, 0x6c, 0x61, 0x74, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x12, + 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x02, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x42, 0x09, 0x5a, + 0x07, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, +} + +var ( + file_hotel_proto_rawDescOnce sync.Once + file_hotel_proto_rawDescData = file_hotel_proto_rawDesc +) + +func file_hotel_proto_rawDescGZIP() []byte { + file_hotel_proto_rawDescOnce.Do(func() { + file_hotel_proto_rawDescData = protoimpl.X.CompressGZIP(file_hotel_proto_rawDescData) + }) + return file_hotel_proto_rawDescData +} + +var file_hotel_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_hotel_proto_goTypes = []interface{}{ + (*Hotel)(nil), // 0: protoproto.hotel +} +var file_hotel_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_hotel_proto_init() } +func file_hotel_proto_init() { + if File_hotel_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_hotel_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Hotel); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_hotel_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_hotel_proto_goTypes, + DependencyIndexes: file_hotel_proto_depIdxs, + MessageInfos: file_hotel_proto_msgTypes, + }.Build() + File_hotel_proto = out.File + file_hotel_proto_rawDesc = nil + file_hotel_proto_goTypes = nil + file_hotel_proto_depIdxs = nil +} diff --git a/offscript/protobufs/protobuf/hotel.pb.go b/offscript/protobufs/protobuf/hotel.pb.go new file mode 100644 index 0000000..a18dece --- /dev/null +++ b/offscript/protobufs/protobuf/hotel.pb.go @@ -0,0 +1,180 @@ +// Code generated by protoc-gen-go. DO NOT EDIT. +// versions: +// protoc-gen-go v1.31.0 +// protoc v4.25.1 +// source: hotel.proto + +package protobuf + +import ( + protoreflect "google.golang.org/protobuf/reflect/protoreflect" + protoimpl "google.golang.org/protobuf/runtime/protoimpl" + reflect "reflect" + sync "sync" +) + +const ( + // Verify that this generated code is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion) + // Verify that runtime/protoimpl is sufficiently up-to-date. + _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20) +) + +type Hotel struct { + state protoimpl.MessageState + sizeCache protoimpl.SizeCache + unknownFields protoimpl.UnknownFields + + Id int32 `protobuf:"varint,1,opt,name=id,proto3" json:"id,omitempty"` + Name string `protobuf:"bytes,2,opt,name=name,proto3" json:"name,omitempty"` + Address string `protobuf:"bytes,3,opt,name=address,proto3" json:"address,omitempty"` + Lattitude float32 `protobuf:"fixed32,4,opt,name=lattitude,proto3" json:"lattitude,omitempty"` + Longitude float32 `protobuf:"fixed32,5,opt,name=longitude,proto3" json:"longitude,omitempty"` +} + +func (x *Hotel) Reset() { + *x = Hotel{} + if protoimpl.UnsafeEnabled { + mi := &file_hotel_proto_msgTypes[0] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) + } +} + +func (x *Hotel) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*Hotel) ProtoMessage() {} + +func (x *Hotel) ProtoReflect() protoreflect.Message { + mi := &file_hotel_proto_msgTypes[0] + if protoimpl.UnsafeEnabled && x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use Hotel.ProtoReflect.Descriptor instead. +func (*Hotel) Descriptor() ([]byte, []int) { + return file_hotel_proto_rawDescGZIP(), []int{0} +} + +func (x *Hotel) GetId() int32 { + if x != nil { + return x.Id + } + return 0 +} + +func (x *Hotel) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *Hotel) GetAddress() string { + if x != nil { + return x.Address + } + return "" +} + +func (x *Hotel) GetLattitude() float32 { + if x != nil { + return x.Lattitude + } + return 0 +} + +func (x *Hotel) GetLongitude() float32 { + if x != nil { + return x.Longitude + } + return 0 +} + +var File_hotel_proto protoreflect.FileDescriptor + +var file_hotel_proto_rawDesc = []byte{ + 0x0a, 0x0b, 0x68, 0x6f, 0x74, 0x65, 0x6c, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x0a, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x81, 0x01, 0x0a, 0x05, 0x68, 0x6f, + 0x74, 0x65, 0x6c, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x05, 0x52, + 0x02, 0x69, 0x64, 0x12, 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, + 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x12, 0x18, 0x0a, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, + 0x73, 0x73, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07, 0x61, 0x64, 0x64, 0x72, 0x65, 0x73, + 0x73, 0x12, 0x1c, 0x0a, 0x09, 0x6c, 0x61, 0x74, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x04, + 0x20, 0x01, 0x28, 0x02, 0x52, 0x09, 0x6c, 0x61, 0x74, 0x74, 0x69, 0x74, 0x75, 0x64, 0x65, 0x12, + 0x1c, 0x0a, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x18, 0x05, 0x20, 0x01, + 0x28, 0x02, 0x52, 0x09, 0x6c, 0x6f, 0x6e, 0x67, 0x69, 0x74, 0x75, 0x64, 0x65, 0x42, 0x0c, 0x5a, + 0x0a, 0x2e, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x62, 0x06, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x33, +} + +var ( + file_hotel_proto_rawDescOnce sync.Once + file_hotel_proto_rawDescData = file_hotel_proto_rawDesc +) + +func file_hotel_proto_rawDescGZIP() []byte { + file_hotel_proto_rawDescOnce.Do(func() { + file_hotel_proto_rawDescData = protoimpl.X.CompressGZIP(file_hotel_proto_rawDescData) + }) + return file_hotel_proto_rawDescData +} + +var file_hotel_proto_msgTypes = make([]protoimpl.MessageInfo, 1) +var file_hotel_proto_goTypes = []interface{}{ + (*Hotel)(nil), // 0: protoproto.hotel +} +var file_hotel_proto_depIdxs = []int32{ + 0, // [0:0] is the sub-list for method output_type + 0, // [0:0] is the sub-list for method input_type + 0, // [0:0] is the sub-list for extension type_name + 0, // [0:0] is the sub-list for extension extendee + 0, // [0:0] is the sub-list for field type_name +} + +func init() { file_hotel_proto_init() } +func file_hotel_proto_init() { + if File_hotel_proto != nil { + return + } + if !protoimpl.UnsafeEnabled { + file_hotel_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { + switch v := v.(*Hotel); i { + case 0: + return &v.state + case 1: + return &v.sizeCache + case 2: + return &v.unknownFields + default: + return nil + } + } + } + type x struct{} + out := protoimpl.TypeBuilder{ + File: protoimpl.DescBuilder{ + GoPackagePath: reflect.TypeOf(x{}).PkgPath(), + RawDescriptor: file_hotel_proto_rawDesc, + NumEnums: 0, + NumMessages: 1, + NumExtensions: 0, + NumServices: 0, + }, + GoTypes: file_hotel_proto_goTypes, + DependencyIndexes: file_hotel_proto_depIdxs, + MessageInfos: file_hotel_proto_msgTypes, + }.Build() + File_hotel_proto = out.File + file_hotel_proto_rawDesc = nil + file_hotel_proto_goTypes = nil + file_hotel_proto_depIdxs = nil +} diff --git a/offscript/protobufs/server/main.go b/offscript/protobufs/server/main.go new file mode 100644 index 0000000..716f7a8 --- /dev/null +++ b/offscript/protobufs/server/main.go @@ -0,0 +1,40 @@ +package main + +import ( + "net/http" + + "github.com/samiam2013/learnGo/offscript/protobufs/protobuf" + "google.golang.org/protobuf/proto" +) + +func main() { + // start an http server on port 8080 + hotel := protobuf.Hotel{ + Id: 1, + Name: "Hilton", + Address: "1234 Main St", + Lattitude: 123.456, + Longitude: 45.678, + } + + // listen on port 8080 + srv := &http.Server{ + Addr: ":8080", + Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // write the protobuf to the response + w.Header().Set("Content-Type", "application/x-protobuf") + w.WriteHeader(http.StatusOK) + out, err := proto.Marshal(&hotel) + if err != nil { + panic(err) + } + if _, err := w.Write(out); err != nil { + panic(err) + } + }), + } + if err := srv.ListenAndServe(); err != nil { + panic(err) + } + +}