golang http客户端使用自定义dns

摘自互联网 原文 package main import ( "context" "io/ioutil" "log" "net" "net/http" "time" ) func main() { var ( dnsResolverIP = "8.8.8.8:53" // Google DNS resolver. dnsResolverProto = "udp" // Protocol to use for the DNS resolver dnsResolverTimeoutMs = 5000 // Timeout (ms) for the DNS resolver (optional) ) dialer := &net.Dialer{ Resolver: &net.Resolver{ PreferGo: true, Dial: func(ctx context.Context, network, address string) (net.Conn, error) { d := net.Dialer{ Timeout: time.Duration(dnsResolverTimeoutMs) * time....

August 22, 2022 · 1 min · czyt

go-kratos使用备忘

自定义接口返回内容 正常的响应序列化逻辑通过Response Encoder实现。 错误的序列化逻辑通过ErrorEncoder实现。 注意:自定义Encoder后,可能会遇到零值字段被忽略的情况,可以参考这个issue。具体的解决办法是 proto定义返回内容,然后将生成的类型在encoder中使用。 简单代码大致如下: proto定义 import "google/protobuf/any.proto"; // BaseResponse is the base response message BaseResponse{ int32 code = 1 [json_name = "code"]; google.protobuf.Any data = 2 [json_name = "data"]; } go代码 func CustomResponseEncoder() http.ServerOption { return http.ResponseEncoder(func(w http.ResponseWriter, r *http.Request, i interface{}) error { reply := &v1.BaseResponse{ Code: 0, } if m, ok := i.(proto.Message); ok { payload, err := anypb.New(m) if err != nil { return err } reply....

August 12, 2022 · 22 min · czyt

Protobuf golang小札

Oneof 如果您有许多字段的消息,并且最多可以同时设置一个字段,则可以使用Oneof功能来执行此行为并保存内存。一个字段就像常规字段一样,除了单一共享内存中的所有字段,最多可以同时设置一个字段。设置Oneof的任何成员都会自动清除所有其他成员。 ​ Google protobuf 文档#Oneof 示例proto 创建protoOneof.proto 的proto文件 syntax = "proto3"; package oneof_test; option go_package ='.;oneof'; message WeiboUser{ string user_id = 1; string user_nick = 2; } message DouyinUser{ string auth_token = 1; string nick_name = 2; } message User{ oneof user_source{ string weibo_url = 1; string douyin_url = 2; } oneof user_info{ WeiboUser weibo_user_info = 3; DouyinUser douyin_user_info = 4; } } 使用命令生成go代码 protoc --proto_path=. --go_out=paths=source_relative:./oneof ./protoOneof.proto 生成的protoOneof.pb.go代码如下:...

July 25, 2022 · 12 min · czyt

使用gotests生成表驱动测试

使用gotests可以很方便的生成表驱动测试代码,表驱动测试的具体内容,请参考go官方的wiki。下面是具体的使用方法。 安装 使用下面命令进行安装 go install github.com/cweill/gotests/[email protected] 如果是go1.16之前的版本,可以使用命令 go get -u github.com/cweill/gotests/...来进行安装。 使用 gotests支持的参数如下: Usage of C:\Users\czyt\go\bin\gotests.exe: -all generate tests for all functions and methods -excl string regexp. generate tests for functions and methods that don't match. Takes precedence over -only, -exported, and -all -exported generate tests for exported functions and methods. Takes precedence over -only and -all -i print test inputs in error messages -nosubtests disable generating tests using the Go 1....

July 22, 2022 · 2 min · czyt

ETCD一键安装脚本

最近要使用ETCD,脚本根据官方GitHub脚本修改而来 #!/usr/bin/bash ETCD_VER=v3.5.4 # choose either URL GOOGLE_URL=https://storage.googleapis.com/etcd GITHUB_URL=https://fastgit.czyt.tech/https://github.com/etcd-io/etcd/releases/download ARCH=linux-arm64 DOWNLOAD_URL=${GITHUB_URL} INSTALL_DIR=/opt/etcd rm -f /tmp/etcd-${ETCD_VER}-${ARCH}.tar.gz rm -rf ${INSTALL_DIR} && mkdir -p ${INSTALL_DIR} curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-${ARCH}.tar.gz -o /tmp/etcd-${ETCD_VER}-${ARCH}.tar.gz tar xzvf /tmp/etcd-${ETCD_VER}-${ARCH}.tar.gz -C ${INSTALL_DIR} --strip-components=1 rm -f /tmp/etcd-${ETCD_VER}-${ARCH}.tar.gz ${INSTALL_DIR}/etcd --version ${INSTALL_DIR}/etcdctl version ${INSTALL_DIR}/etcdutl version 其中的ARCH请根据实际情况修改。 参考 https://etcd.io/docs/v3.5/demo/

July 21, 2022 · 1 min · czyt

Golang Expr不完全指南

安装 库的说明 Expr package provides an engine that can compile and evaluate expressions. An expression is a one-liner that returns a value (mostly, but not limited to, booleans). It is designed for simplicity, speed and safety. The purpose of the package is to allow users to use expressions inside configuration for more complex logic. It is a perfect candidate for the foundation of a business rule engine. 安装 go get -u /github....

July 13, 2022 · 9 min · czyt

Golang DSL参考

ANTLR 4 图书 The definitive ANTLR 4 reference (2014) 英文版下载 中文版下载 文章 使用ANTLR和Go实现DSL入门 手把手教你使用ANTLR和Go实现一门DSL语言part1 part2part3part4part5 Parsing with ANTLR 4 and Go 实例代码 bilibili gengine link go-zero link grule-rule-engine Others 图书 Writing A Compiler In Go Writing an Interpreter in Go µGo语言实现——从头开发一个迷你Go语言编译器 文章 Build your own DSL with Go & HCL How to Write Syntax Tree-Based Domain-Specific Languages in Go Handwritten Parsers & Lexers in Go goyacc实战 TiDB SQL Parser 的实现...

July 11, 2022 · 1 min · czyt

Vue 相关资源

Vue - The Complete Guide (incl. Router & Composition API) The Vue.js 3 Masterclass TypeScript Friendly Vue 3 Vue.js Fundamentals

June 30, 2022 · 1 min · czyt

Golang io.Pipe 使用

介绍 // Pipe creates a synchronous in-memory pipe. // It can be used to connect code expecting an io.Reader // with code expecting an io.Writer. // // Reads and Writes on the pipe are matched one to one // except when multiple Reads are needed to consume a single Write. // That is, each Write to the PipeWriter blocks until it has satisfied // one or more Reads from the PipeReader that fully consume // the written data....

June 24, 2022 · 3 min · czyt

go embed 使用小记

​ go embed 是go 1.16 开始添加的特性,允许嵌入文件及文件夹,在Go程序中进行使用。官方还为此添加了embed.FS的对象。下面将常用的使用场景进行简单列举: 嵌入单个文件 官方的例子 嵌入文件并绑定到字符串变量 import _ "embed" //go:embed hello.txt var s string print(s) 嵌入文件并绑定到字节变量 import _ "embed" //go:embed hello.txt var b []byte print(string(b)) 嵌入文件并绑定到文件对象 import "embed" //go:embed hello.txt var f embed.FS data, _ := f.ReadFile("hello.txt") print(string(data)) 嵌入目录 嵌入时,可以在多行或者一行输入要嵌入的文件和文件夹。 package server import "embed" // content holds our static web server content. //go:embed image/* template/* //go:embed html/index.html var content embed.FS 在匹配文件夹时,embed会嵌入包括子目录下的所有除.和_开头的文件(递归),所以上面的代码大致等价于下面的代码: // content is our static web server content....

June 23, 2022 · 1 min · czyt