随笔分类 -  Go

上一页 1 2 3 4 5 下一页

go 结构体类型与空接口类型互转
摘要:package main import "fmt" type Stu struct { age int } func main() { var stuInterface interface{} = &Stu{ age: 10, } if stu, ok := stuInterface.(*Stu); 阅读全文

posted @ 2024-04-28 08:39 王景迁 阅读(22) 评论(0) 推荐(0)

go.mod引用git仓库依赖
摘要:方式1:使用commit id go get k8s.io/client-go@1518fca9f06c6a73fc091535b8966c71704e657b 方式2:使用分支 go get k8s.io/client-go@master 使用分支也是commit id。 方式3:go.mod中写 阅读全文

posted @ 2024-04-11 21:14 王景迁 阅读(163) 评论(0) 推荐(0)

go结构体重写String方法从而自定义打印内容
摘要:package main import "fmt" type student struct { name string age int } func (s student) String() string { return fmt.Sprintf("{name is %s, age is %d}", 阅读全文

posted @ 2024-04-08 22:22 王景迁 阅读(215) 评论(0) 推荐(0)

go实现LRU
摘要:package main import "fmt" type LRUCache struct { length int cap int cache map[interface{}]*DoubleLinkNode head *DoubleLinkNode tail *DoubleLinkNode } 阅读全文

posted @ 2024-03-28 21:06 王景迁 阅读(24) 评论(0) 推荐(0)

go 操作ip
摘要:获取网卡IPv4地址 package main import ( "fmt" "net" ) func main() { ifname := "ens33" netIf, err := net.InterfaceByName(ifname) if err != nil { fmt.Printf("g 阅读全文

posted @ 2024-03-22 08:57 王景迁 阅读(38) 评论(0) 推荐(0)

go 同一个https端口实现多种认证方式
摘要:参考kube-apiserver,访问https端口时,-k允许跳过默认的证书认证,从而实现多种认证方式。 package main import ( "crypto/tls" "fmt" "net/http" klog "k8s.io/klog/v2" ) func healthCheck(w h 阅读全文

posted @ 2024-02-26 09:08 王景迁 阅读(64) 评论(0) 推荐(0)

go ticker源码分析
摘要:结构体定义 Timer是一次性定时器,Ticker是周期性定时器,实现相同,系统协程处理所有定时器。 Ticker结构体 C是容量1的channel,如果里面数据没被取走,那么丢失下一个周期事件。 runtimeTimer结构体 这里的回调函数是,向C中存入当前时间。 timersBuckets 数 阅读全文

posted @ 2024-02-05 17:07 王景迁 阅读(36) 评论(0) 推荐(0)

go dns解析参数配置
摘要:// lookup_dns.go package main import ( "net" "time" "k8s.io/klog/v2" ) func main() { dns := "kubernetes.default" if _, err := net.LookupHost(dns); err 阅读全文

posted @ 2024-02-04 23:14 王景迁 阅读(133) 评论(0) 推荐(0)

通过strace来分析go域名解析过程
摘要:strace用于跟踪进程运行时系统调用和接收的信号,支持跟踪要启动和已运行的进程。 参数 含义 -T 显示系统调用消耗时间 -tt 显示毫秒级别时间 -p 指定跟踪的进程 -v 输出环境变量等上下文内容 例如,查看进程对应的线程系统调用。 strace -fp [pid] -T -tt 2>&1 p 阅读全文

posted @ 2024-01-31 08:11 王景迁 阅读(54) 评论(0) 推荐(0)

main.go无法引用同目录下其他文件的函数和变量以及常量
摘要:// main_call_other_file_const_define.go package main const ( Num = 10 ) // main_call_other_file_const.go package main import "fmt" func main() { fmt.P 阅读全文

posted @ 2024-01-28 11:27 王景迁 阅读(247) 评论(0) 推荐(0)

go依赖和编译报错
摘要:问题1:go: updates to go.mod needed; to update it: go mod tidy方法:go clean -modcache && go mod tidy 问题2:go: updates to go.mod needed, disabled by -mod=ven 阅读全文

posted @ 2024-01-15 22:56 王景迁 阅读(751) 评论(0) 推荐(0)

http包中的ListenAndServe函数是阻塞式的
摘要:package main import ( "fmt" "net/http" ) func main() { fmt.Println("before listen") http.ListenAndServe(":1000", nil) fmt.Println("after listen") } 执行 阅读全文

posted @ 2023-11-01 21:40 王景迁 阅读(173) 评论(0) 推荐(0)

设置go下载依赖代理
摘要:go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct 阅读全文

posted @ 2023-09-29 19:54 王景迁 阅读(122) 评论(0) 推荐(0)

go 时间错误
摘要:invalid operation: n * time.Second (mismatched types int and time.Duration) package main import "time" func main() { n := 1 time.Sleep(n * time.Second 阅读全文

posted @ 2023-07-15 10:28 王景迁 阅读(79) 评论(0) 推荐(0)

go panic
摘要:panic可能原因 1. 空指针2. 直接调用panic函数3. 数组越界4. map读写并发(recover不能恢复) core dump程序出现段错误时出现的错误文件,通过该文件确认错误的位置。程序因段错误异常终止时打印堆栈信息// 开启core dump功能,不限制core文件大小ulimit 阅读全文

posted @ 2023-07-15 10:24 王景迁 阅读(115) 评论(0) 推荐(0)

go strings.Builder
摘要:字符串拼接和strings.Buffer缺点 Go里面的字符串是常量,对字符串的修改会重新申请内存地址。虽然bytes.Buffer避免了字符串修改过程中的内存申请,但是最后从[]byte转成字符串时会重新内存申请。从Go 1.10开始,提供了性能更好的方法strings.Builder,与byte 阅读全文

posted @ 2023-07-15 10:20 王景迁 阅读(250) 评论(0) 推荐(0)

patrickmn.gocache
摘要:一句话概括 基于内存的KV缓存,支持删除和过期以及持久化到文件并恢复。 使用示例 go.mod增加依赖require github.com/patrickmn/go-cache v2.1.0+incompatible package main import ( "log" "time" "githu 阅读全文

posted @ 2023-07-15 10:15 王景迁 阅读(79) 评论(0) 推荐(0)

切片
摘要:结构体定义 runtime/slice.go文件中 type slice struct { array unsafe.Pointer // 数组的指针 len int cap int } 扩容 通过append方法添加数据,返回一个新的slice对象,地址和之前不一样。但是原来元素地址是不变的,直到 阅读全文

posted @ 2023-07-15 10:11 王景迁 阅读(40) 评论(0) 推荐(0)

go map
摘要:map内存模型 // A header for a Go map. type hmap struct { // 元素个数,调用 len(map) 时,直接返回此值 count int flags uint8 // buckets 的对数 log_2 B uint8 // overflow 的 buc 阅读全文

posted @ 2023-07-15 10:08 王景迁 阅读(56) 评论(0) 推荐(0)

go context
摘要:使用场景 在协程之间传递上下文 context接口 type Context interface { // 返回绑定当前context的任务取消的截止时间 // 如果没有设定期限,将返回ok == false Deadline() (deadline time.Time, ok bool) // 绑 阅读全文

posted @ 2023-07-15 10:04 王景迁 阅读(27) 评论(0) 推荐(0)

上一页 1 2 3 4 5 下一页

导航