为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=....

个人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....

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....

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!") }

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 !...

Golang通过ssh连接数据库

Mysql Postgresql

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) 多次匹配:...

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....

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

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