摘要:package main import "fmt" type Stu struct { age int } func main() { var stuInterface interface{} = &Stu{ age: 10, } if stu, ok := stuInterface.(*Stu);
阅读全文
摘要:方式1:使用commit id go get k8s.io/client-go@1518fca9f06c6a73fc091535b8966c71704e657b 方式2:使用分支 go get k8s.io/client-go@master 使用分支也是commit id。 方式3:go.mod中写
阅读全文
摘要: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}",
阅读全文
摘要:package main import "fmt" type LRUCache struct { length int cap int cache map[interface{}]*DoubleLinkNode head *DoubleLinkNode tail *DoubleLinkNode }
阅读全文
摘要:获取网卡IPv4地址 package main import ( "fmt" "net" ) func main() { ifname := "ens33" netIf, err := net.InterfaceByName(ifname) if err != nil { fmt.Printf("g
阅读全文
摘要:参考kube-apiserver,访问https端口时,-k允许跳过默认的证书认证,从而实现多种认证方式。 package main import ( "crypto/tls" "fmt" "net/http" klog "k8s.io/klog/v2" ) func healthCheck(w h
阅读全文
摘要:结构体定义 Timer是一次性定时器,Ticker是周期性定时器,实现相同,系统协程处理所有定时器。 Ticker结构体 C是容量1的channel,如果里面数据没被取走,那么丢失下一个周期事件。 runtimeTimer结构体 这里的回调函数是,向C中存入当前时间。 timersBuckets 数
阅读全文
摘要:// lookup_dns.go package main import ( "net" "time" "k8s.io/klog/v2" ) func main() { dns := "kubernetes.default" if _, err := net.LookupHost(dns); err
阅读全文
摘要:strace用于跟踪进程运行时系统调用和接收的信号,支持跟踪要启动和已运行的进程。 参数 含义 -T 显示系统调用消耗时间 -tt 显示毫秒级别时间 -p 指定跟踪的进程 -v 输出环境变量等上下文内容 例如,查看进程对应的线程系统调用。 strace -fp [pid] -T -tt 2>&1 p
阅读全文
摘要:// 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
阅读全文
摘要:问题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
阅读全文
摘要:package main import ( "fmt" "net/http" ) func main() { fmt.Println("before listen") http.ListenAndServe(":1000", nil) fmt.Println("after listen") } 执行
阅读全文
摘要:go env -w GO111MODULE=on go env -w GOPROXY=https://goproxy.cn,direct
阅读全文
摘要:invalid operation: n * time.Second (mismatched types int and time.Duration) package main import "time" func main() { n := 1 time.Sleep(n * time.Second
阅读全文
摘要:panic可能原因 1. 空指针2. 直接调用panic函数3. 数组越界4. map读写并发(recover不能恢复) core dump程序出现段错误时出现的错误文件,通过该文件确认错误的位置。程序因段错误异常终止时打印堆栈信息// 开启core dump功能,不限制core文件大小ulimit
阅读全文
摘要:字符串拼接和strings.Buffer缺点 Go里面的字符串是常量,对字符串的修改会重新申请内存地址。虽然bytes.Buffer避免了字符串修改过程中的内存申请,但是最后从[]byte转成字符串时会重新内存申请。从Go 1.10开始,提供了性能更好的方法strings.Builder,与byte
阅读全文
摘要:一句话概括 基于内存的KV缓存,支持删除和过期以及持久化到文件并恢复。 使用示例 go.mod增加依赖require github.com/patrickmn/go-cache v2.1.0+incompatible package main import ( "log" "time" "githu
阅读全文
摘要:结构体定义 runtime/slice.go文件中 type slice struct { array unsafe.Pointer // 数组的指针 len int cap int } 扩容 通过append方法添加数据,返回一个新的slice对象,地址和之前不一样。但是原来元素地址是不变的,直到
阅读全文
摘要:map内存模型 // A header for a Go map. type hmap struct { // 元素个数,调用 len(map) 时,直接返回此值 count int flags uint8 // buckets 的对数 log_2 B uint8 // overflow 的 buc
阅读全文
摘要:使用场景 在协程之间传递上下文 context接口 type Context interface { // 返回绑定当前context的任务取消的截止时间 // 如果没有设定期限,将返回ok == false Deadline() (deadline time.Time, ok bool) // 绑
阅读全文