摘要:序列化后输出。 package main import ( "encoding/json" "fmt" ) type Student struct { Age int Name string } func printStructPointerSlice(data interface{}) strin
阅读全文
摘要:方式1:debug.Stack函数 调用runtime.Stack函数,无需指定参数,输出当前goroutine的栈信息。源码在runtime/debug/stack.go中: func Stack() []byte { buf := make([]byte, 1024) for { n := ru
阅读全文
摘要:google.golang.org/grpc v1.66.0 conn, err := grpc.NewClient("127.0.0.1:1000", grpc.WithTransportCredentials(insecure.NewCredentials())) 启动后就会看到该连接。 创建g
阅读全文
摘要:基于推特雪花算法生成定长id,属于int64类型。 1 Bit Unused | 41 Bit Timestamp | 10 Bit NodeID | 12 Bit Sequence ID1 bit未使用,默认是0。41bit存储毫秒级时间戳,当前时间与Nov 04 2010 01:42:54 UT
阅读全文
摘要:package main import ( "encoding/binary" "fmt" ) func main() { // 127.0.0.1主机字节序 var value uint32 = 2130706433 // 主机字节序->网络字节序 bigData := make([]byte,
阅读全文
摘要:go没有继承。类似于Java继承,SecurityGroup类继承了SecurityGroupRule列表类。 type SecurityGroup struct { Id string Name string Rules []SecurityGroupRule } type SecurityGro
阅读全文
摘要:问题:执行go mod vendor后,项目中增加了import依赖,不会增加go:generate go run后面依赖。解决:为了避免每次go mod拉取依赖,先import它,然后执行go mod vendor,最后删除该import。
阅读全文
摘要:相比于klog,logrus支持输出json日志,但是默认time不在最前面,而在最后,因为日志输出时按照key字母顺序排序。 go get github.com/sirupsen/logrus go get github.com/natefinch/lumberjack package main
阅读全文
摘要:问题1:被序列化的结构体首字母必须大写 type Student struct { sex string age int } 如果被序列化的结构体首字母不大写,那么序列化结果是空。 告警内容 struct type 'test/json_config.Student' doesn't have an
阅读全文
摘要:package main import ( "encoding/json" "log" ) type Student struct { Sex string `json:"sex"` Age int `json:"age"` } func main() { s1 := &Student{ Sex:
阅读全文
摘要:安装依赖 go get github.com/onsi/ginkgo/v2/ginkgo go install github.com/onsi/ginkgo/v2/ginkgo go get github.com/onsi/gomega 运行用例 mkdir test cd test ginkgo
阅读全文
摘要:安装依赖和工具 # ubuntu安装protobuf apt install libprotobuf-dev protobuf-compiler protoc-gen-go protoc-gen-go-grpc -y # 查看protobuf版本 protoc --version # 安装grpcu
阅读全文
摘要:使用reflect的DeepEqual方法,列表匹配时按顺序逐个比较。 场景1:结构体列表按顺序匹配(直接比较) package main import ( "fmt" "reflect" ) type Student struct { Age int Score int } func main()
阅读全文
摘要:常见类型的默认排序实现 go sort包默认支持int(sort.Ints(x []int))、float64s(sort.Float64s(x []float64))、string(sort.Strings(x []string))从小到大排序,反序使用类似于sort.Sort(sort.Reve
阅读全文
摘要:方式1:只用参数值 package main import ( "fmt" "os" ) func main() { // os.Args是[]string for k, v := range os.Args { fmt.Printf("args[%d]=[%s]\n", k, v) } } 方式2
阅读全文
摘要:package main import "fmt" type Test string func (t Test) String() string { return "test string" } func (t Test) Error() string { return "test error" }
阅读全文
摘要:package main import ( "errors" "fmt" ) func main() { err := errors.New("err1") defer fmt.Println(err.Error()) err = errors.New("err2") } defer只会记住执行到此
阅读全文
摘要:common.go package common const ( SocketPath = "/run/test.sock" ) type Request struct { Age int `json:"age"` } type Response struct { Err string `json:
阅读全文
摘要:cilium 1.15.1 MAC地址的长度是48位(6个字节),表示为12个16进制数,例如00:01:02:03:04:05。 生成随机mac package main import ( "crypto/rand" "fmt" "net" ) // MAC address is an net.H
阅读全文
摘要:go get k8s.io/klog/v2 go get github.com/shirou/gopsutil package main import ( "fmt" "time" "github.com/shirou/gopsutil/host" "k8s.io/klog/v2" ) func m
阅读全文