上一页 1 ··· 37 38 39 40 41 42 43 44 45 ··· 53 下一页

2023年7月15日

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 王景迁 阅读(67) 评论(0) 推荐(0)

go panic

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

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

go strings.Builder

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

posted @ 2023-07-15 10:20 王景迁 阅读(236) 评论(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 王景迁 阅读(72) 评论(0) 推荐(0)

切片

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

posted @ 2023-07-15 10:11 王景迁 阅读(35) 评论(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 王景迁 阅读(48) 评论(0) 推荐(0)

go context

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

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

优先队列(基于二叉树的堆)

摘要: 代码出处 Go SDK container/heap/heap.go Interface 接口定义 type Interface interface { sort.Interface Push(x interface{}) // add x as element Len() Pop() interf 阅读全文

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

go引用类型和值类型

摘要: 引用类型引用类型有字典类型、通道类型、函数类型、切片类型、接口类型和指针类型。 值类型值类型有基础数据类型和结构体类型以及数组类型。 阅读全文

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

go单元测试显示测试用例代码覆盖率

摘要: 执行单个目录所有测试用例并生成覆盖率 go test -gcflags=all=-l -coverprofile=$GOPATH/bin/c.out . go tool cover -html=$GOPATH/bin/c.out -o=$GOPATH/bin/tag.html 下面场景相同go to 阅读全文

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

上一页 1 ··· 37 38 39 40 41 42 43 44 45 ··· 53 下一页

导航