为Kratos prtobuf文件添加多种编译输出

Csharp 安装Grpc.tools https://www.nuget.org/packages/Grpc.Tools/ 下载解压 nupkg文件(改扩展名为zip),也可以使用附件的7z包 解压 找到tools中对应系统架构的软件,设置下环境变量,让系统可以找到就行。 Linux 需要创建一个符号链接 ln -s `which grpc_csharp_plugin` /usr/bin/protoc-gen-grpc-csharp 修改Kratos项目的Make文件 在api这个make任务中添加下面内容 --csharp_out=./api/pipe/v1 \ --grpc-csharp_out=./api/pipe/v1 \ 完整内容为 .PHONY: api # generate api proto api: protoc --proto_path=./api \ --proto_path=./third_party \ --go_out=paths=source_relative:./api \ --go-http_out=paths=source_relative:./api \ --go-grpc_out=paths=source_relative:./api \ --csharp_out=./api/pipe/v1 \ --grpc-csharp_out=./api/pipe/v1 \ --openapi_out==paths=source_relative:. \ 参考 https://github.com/grpc/grpc/blob/master/src/csharp/BUILD-INTEGRATION.md 📎tools.7z Python 安装必要包 pip install grpclib protobuf 查询路径 which protoc-gen-grpclib_python 或者 which protoc-gen-python_grpc我这里返回信息如下: ➜ czyt which protoc-gen-grpclib_python /usr/sbin/protoc-gen-grpclib_python 如法炮制,创建软链接 ln -s /usr/sbin/protoc-gen-grpclib_python /usr/sbin/protoc-gen-grpc_python 修改Makefile 添加下面的内容,再执行make api生成api即可。 --python_out=./api \ --grpc_python_out=./api \ Dart 安装插件 dart pub global activate protoc_plugin 将插件工具加入环境变量 export PATH="$PATH:$HOME/.pub-cache/bin" 脚本添加--dart_out=选项 --dart_out=./api \ TypeScript 纯typescript类 ts-proto typescript类+服务 安装proto工具 go get go.einride.tri p tech/protoc-gen-typescript-http ...

2022-04-08 · 1 分钟 · czyt

个人Golang环境安装快速设置

下载 官方下载 https://go.dev/dl/ Google 香港镜像 Golang Downloads Mirrors 更多请参考 Thanks Mirror 环境设置 设置proxy go env -w GOPROXY=https://goproxy.io,https://goproxy.cn,direct 安装相关工具 进程工具 goreman go install github.com/mattn/goreman@latest 框架Cli kratos go install github.com/go-kratos/kratos/cmd/kratos/v2@latest wire go install github.com/google/wire/cmd/wire@latest ent go install entgo.io/ent/cmd/ent@latest entimport go install ariga.io/entimport/cmd/entimport@latest entproto go install entgo.io/contrib/entproto/cmd/entproto@latest 代码Lint golangci-lint go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest golines go install github.com/segmentio/golines@latest Go into the Goland settings and click “Tools” -> “File Watchers” then click the plus to create a new file watcher Set the following properties and confirm by clicking OK: Name: golines File type: Go files Scope: Project Files Program: golines Arguments: $FilePath$ -w Output paths to refresh: $FilePath$ gofumpt go install mvdan.cc/gofumpt@latest goland设置 ...

2022-03-24 · 2 分钟 · czyt

grpc-golang windows环境搭建说明

下载protoc,打开链接 下载后将对应的文件解压到gopath的bin目录。 下载protoc的golang插件。下载地址 链接 下载后放在protoc的同级目录(需要改扩展名为exe) 测试,定义一个Proto syntax = "proto3"; option go_package = ".;hello"; package main; message String { string value = 1; } 然后执行命令 protoc hello.proto --go_out=. ,大功告成,生成的文件内容如下: // Code generated by protoc-gen-go. DO NOT EDIT. // versions: // protoc-gen-go v1.24.0-devel // protoc v3.12.3 // source: hello.proto package hello import ( proto "github.com/golang/protobuf/proto" 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) ) // This is a compile-time assertion that a sufficiently up-to-date version // of the legacy proto package is being used. const _ = proto.ProtoPackageIsVersion4 type String struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields Value string `protobuf:"bytes,1,opt,name=value,proto3" json:"value,omitempty"` } func (x *String) Reset() { *x = String{} if protoimpl.UnsafeEnabled { mi := &file_hello_proto_msgTypes[0] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } } func (x *String) String() string { return protoimpl.X.MessageStringOf(x) } func (*String) ProtoMessage() {} func (x *String) ProtoReflect() protoreflect.Message { mi := &file_hello_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 String.ProtoReflect.Descriptor instead. func (*String) Descriptor() ([]byte, []int) { return file_hello_proto_rawDescGZIP(), []int{0} } func (x *String) GetValue() string { if x != nil { return x.Value } return "" } var File_hello_proto protoreflect.FileDescriptor var file_hello_proto_rawDesc = []byte{ 0x0a, 0x0b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x04, 0x6d, 0x61, 0x69, 0x6e, 0x22, 0x1e, 0x0a, 0x06, 0x53, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x12, 0x14, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x42, 0x09, 0x5a, 0x07, 0x2e, 0x3b, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( file_hello_proto_rawDescOnce sync.Once file_hello_proto_rawDescData = file_hello_proto_rawDesc ) func file_hello_proto_rawDescGZIP() []byte { file_hello_proto_rawDescOnce.Do(func() { file_hello_proto_rawDescData = protoimpl.X.CompressGZIP(file_hello_proto_rawDescData) }) return file_hello_proto_rawDescData } var file_hello_proto_msgTypes = make([]protoimpl.MessageInfo, 1) var file_hello_proto_goTypes = []interface{}{ (*String)(nil), // 0: main.String } var file_hello_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_hello_proto_init() } func file_hello_proto_init() { if File_hello_proto != nil { return } if !protoimpl.UnsafeEnabled { file_hello_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} { switch v := v.(*String); 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_hello_proto_rawDesc, NumEnums: 0, NumMessages: 1, NumExtensions: 0, NumServices: 0, }, GoTypes: file_hello_proto_goTypes, DependencyIndexes: file_hello_proto_depIdxs, MessageInfos: file_hello_proto_msgTypes, }.Build() File_hello_proto = out.File file_hello_proto_rawDesc = nil file_hello_proto_goTypes = nil file_hello_proto_depIdxs = nil } 注意: ...

2022-02-28 · 3 分钟 · czyt

golang webserver with genergic base64 /favicon.ico

package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/favicon.ico", favicon) http.HandleFunc("/", hello) fmt.Printf("listening on http://localhost:8000/\n") http.ListenAndServe("localhost:8000", nil) } func favicon(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s\n", r.RequestURI) w.Header().Set("Content-Type", "image/x-icon") w.Header().Set("Cache-Control", "public, max-age=7776000") fmt.Fprintln(w, "data:image/x-icon;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQEAYAAABPYyMiAAAABmJLR0T///////8JWPfcAAAACXBIWXMAAABIAAAASABGyWs+AAAAF0lEQVRIx2NgGAWjYBSMglEwCkbBSAcACBAAAeaR9cIAAAAASUVORK5CYII=\n") } func hello(w http.ResponseWriter, r *http.Request) { fmt.Printf("%s\n", r.RequestURI) fmt.Fprintln(w, "Hello, World!") }

2022-02-23 · 1 分钟 · czyt

Golang嵌入可执行程序

reddit链接 On Linux it might be possible to use the memfd_create system call, but that’s not portable to other operating systems. need go 1.16 + package main import ( _ "embed" "log" "os" "os/exec" "strconv" "golang.org/x/sys/unix" ) //go:embed binary var embeddedBinary []byte func main() { fd, err := unix.MemfdCreate("embedded_binary", 0) if err != nil { log.Fatal(err) } path := "/proc/" + strconv.Itoa(os.Getpid()) + "/fd/" + strconv.Itoa(int(fd)) err = os.WriteFile(path, embeddedBinary, 0755) if err != nil { log.Fatal(err) } cmd := exec.Command(path) cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr err = cmd.Run() if err != nil { log.Fatal(err) } } You should be able to replace that os.WriteFile with f := os.NewFile(fd, “memfd”); _, err := f.Write(embeddedBinary) for a simpler end result. ...

2022-02-23 · 1 分钟 · czyt

Golang通过ssh连接数据库

Mysql View this Gist on GitHub Postgresql View this Gist on GitHub

2022-02-23 · 1 分钟 · czyt

golang正则表达式小札

准备工作 golang正则需要引入包中的regexp包。 import ( "regexp" ) 如果需要复用正则表达式对象来提高性能,可以通过Compile() 或者 MustCompile()创建一个编译好的正则表达式对象。 支持的表达式 单个匹配: . any character, possibly including newline (flag s=true) [xyz] character class [^xyz] negated character class \d Perl character class \D negated Perl character class [[:alpha:]] ASCII character class [[:^alpha:]] negated ASCII character class \pN Unicode character class (one-letter name) \p{Greek} Unicode character class \PN negated Unicode character class (one-letter name) \P{Greek} negated Unicode character class 组合匹配: xy x followed by y x|y x or y (prefer x) 多次匹配: ...

2021-07-27 · 3 分钟 · czyt

golang不创建临时文件上传网络文件

func UploadFromUrl(uploadUrl string, resUrl string,postFileName string, submitField string) error { method := "POST" payload := &bytes.Buffer{} writer := multipart.NewWriter(payload) if res, err := http.Get(resUrl); err != nil { return err } else { defer func() { if res != nil { _ = res.Body.Close() } }() part, _ := writer.CreateFormFile(submitField, postFileName) if _, copyErr := io.Copy(part, res.Body); copyErr != nil { return copyErr } if err := writer.Close(); err != nil { return err } } client := &http.Client{} req, err := http.NewRequest(method, uploadUrl, payload) if err != nil { return err } req.Header.Set("Content-Type", writer.FormDataContentType()) resp, err := client.Do(req) if err != nil { return err } defer func() { _ = resp.Body.Close() client = nil writer = nil }() return nil }

2021-05-21 · 1 分钟 · czyt

golang检测网络连接是否关闭

_, err := conn.Read(make([]byte, 0)) if err!=io.EOF{ // this connection is invalid logger.W("conn closed....",err) }else{ byt, _:= ioutil.ReadAll(conn); } 注意:net: don’t return io.EOF from zero byte reads issue 参考 https://stackoverflow.com/questions/12741386/how-to-know-tcp-connection-is-closed-in-net-package

2020-06-11 · 1 分钟 · czyt

golang使用官方库实现i18n

示例 package main import ( "fmt" "golang.org/x/text/language" "golang.org/x/text/message" "golang.org/x/text/message/catalog" ) func main() { builder := catalog.NewBuilder() chTag:=language.Make("zh_Hans") engTag:=language.Make("en") builder.SetString(chTag,"hello","您好") builder.SetString(engTag,"hello","Hello") fmt.Println(builder.Languages()) option := message.Catalog(builder) p := message.NewPrinter(chTag,option) p.Printf("hello") p2 := message.NewPrinter(engTag,option) p2.Printf("hello") } 参考 https://zyfdegh.github.io/post/201805-translation-go-i18n https://www.alexedwards.net/blog/i18n-managing-translations https://phrase.com/blog/posts/internationalisation-in-go-with-go-i18n/ https://lokalise.com/blog/go-internationalization-using-go-i18n/ https://go.googlesource.com/proposal/+/master/design/12750-localization.md

2020-06-10 · 1 分钟 · czyt