随笔分类 -  Go

上一页 1 2 3 4 5 下一页

go 打印结构体指针数组
摘要:序列化后输出。 package main import ( "encoding/json" "fmt" ) type Student struct { Age int Name string } func printStructPointerSlice(data interface{}) strin 阅读全文

posted @ 2024-12-30 09:25 王景迁 阅读(34) 评论(0) 推荐(0)

go 输出栈信息
摘要:方式1:debug.Stack函数 调用runtime.Stack函数,无需指定参数,输出当前goroutine的栈信息。源码在runtime/debug/stack.go中: func Stack() []byte { buf := make([]byte, 1024) for { n := ru 阅读全文

posted @ 2024-12-18 09:11 王景迁 阅读(42) 评论(0) 推荐(0)

go grpc默认长连接
摘要:google.golang.org/grpc v1.66.0 conn, err := grpc.NewClient("127.0.0.1:1000", grpc.WithTransportCredentials(insecure.NewCredentials())) 启动后就会看到该连接。 创建g 阅读全文

posted @ 2024-10-22 21:11 王景迁 阅读(350) 评论(0) 推荐(0)

go 基于推特雪花算法生成定长id
摘要:基于推特雪花算法生成定长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 阅读全文

posted @ 2024-10-17 09:20 王景迁 阅读(29) 评论(0) 推荐(0)

go大小端互转
摘要:package main import ( "encoding/binary" "fmt" ) func main() { // 127.0.0.1主机字节序 var value uint32 = 2130706433 // 主机字节序->网络字节序 bigData := make([]byte, 阅读全文

posted @ 2024-10-01 09:43 王景迁 阅读(23) 评论(0) 推荐(0)

go结构体组合
摘要:go没有继承。类似于Java继承,SecurityGroup类继承了SecurityGroupRule列表类。 type SecurityGroup struct { Id string Name string Rules []SecurityGroupRule } type SecurityGro 阅读全文

posted @ 2024-09-28 16:52 王景迁 阅读(21) 评论(0) 推荐(0)

go mod vendor问题
摘要:问题:执行go mod vendor后,项目中增加了import依赖,不会增加go:generate go run后面依赖。解决:为了避免每次go mod拉取依赖,先import它,然后执行go mod vendor,最后删除该import。 阅读全文

posted @ 2024-09-28 16:27 王景迁 阅读(89) 评论(0) 推荐(0)

go logrus输出json日志并转储
摘要:相比于klog,logrus支持输出json日志,但是默认time不在最前面,而在最后,因为日志输出时按照key字母顺序排序。 go get github.com/sirupsen/logrus go get github.com/natefinch/lumberjack package main 阅读全文

posted @ 2024-09-26 21:47 王景迁 阅读(52) 评论(0) 推荐(0)

go json配置
摘要:问题1:被序列化的结构体首字母必须大写 type Student struct { sex string age int } 如果被序列化的结构体首字母不大写,那么序列化结果是空。 告警内容 struct type 'test/json_config.Student' doesn't have an 阅读全文

posted @ 2024-09-26 20:39 王景迁 阅读(26) 评论(0) 推荐(0)

go panic interface conversion interface {} is float64, not int
摘要:package main import ( "encoding/json" "log" ) type Student struct { Sex string `json:"sex"` Age int `json:"age"` } func main() { s1 := &Student{ Sex: 阅读全文

posted @ 2024-09-26 20:23 王景迁 阅读(63) 评论(0) 推荐(0)

ginkgo编写测试用例
摘要:安装依赖 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 阅读全文

posted @ 2024-09-08 16:24 王景迁 阅读(159) 评论(0) 推荐(0)

go 使用grpc和grpcurl
摘要:安装依赖和工具 # ubuntu安装protobuf apt install libprotobuf-dev protobuf-compiler protoc-gen-go protoc-gen-go-grpc -y # 查看protobuf版本 protoc --version # 安装grpcu 阅读全文

posted @ 2024-09-07 10:12 王景迁 阅读(233) 评论(0) 推荐(0)

go 结构体列表比较是否相等
摘要:使用reflect的DeepEqual方法,列表匹配时按顺序逐个比较。 场景1:结构体列表按顺序匹配(直接比较) package main import ( "fmt" "reflect" ) type Student struct { Age int Score int } func main() 阅读全文

posted @ 2024-08-28 21:15 王景迁 阅读(29) 评论(0) 推荐(0)

go 结构体切片自定义排序
摘要:常见类型的默认排序实现 go sort包默认支持int(sort.Ints(x []int))、float64s(sort.Float64s(x []float64))、string(sort.Strings(x []string))从小到大排序,反序使用类似于sort.Sort(sort.Reve 阅读全文

posted @ 2024-08-28 20:58 王景迁 阅读(77) 评论(0) 推荐(0)

go解析命令行的3种方式
摘要:方式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 阅读全文

posted @ 2024-07-05 07:15 王景迁 阅读(75) 评论(0) 推荐(0)

go Error方法优先级高于String方法
摘要:package main import "fmt" type Test string func (t Test) String() string { return "test string" } func (t Test) Error() string { return "test error" } 阅读全文

posted @ 2024-07-02 08:16 王景迁 阅读(16) 评论(0) 推荐(0)

go defer读不到最新error
摘要:package main import ( "errors" "fmt" ) func main() { err := errors.New("err1") defer fmt.Println(err.Error()) err = errors.New("err2") } defer只会记住执行到此 阅读全文

posted @ 2024-06-29 17:22 王景迁 阅读(17) 评论(0) 推荐(0)

go本地unix socket
摘要:common.go package common const ( SocketPath = "/run/test.sock" ) type Request struct { Age int `json:"age"` } type Response struct { Err string `json: 阅读全文

posted @ 2024-06-29 17:14 王景迁 阅读(73) 评论(0) 推荐(0)

go 操作mac
摘要: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 阅读全文

posted @ 2024-06-08 11:05 王景迁 阅读(43) 评论(0) 推荐(0)

go 获取开机时间并根据间隔时间计算
摘要: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 阅读全文

posted @ 2024-06-04 21:12 王景迁 阅读(77) 评论(0) 推荐(0)

上一页 1 2 3 4 5 下一页

导航