随笔分类 -  Go

摘要:package main import ( "fmt" "math" "runtime" "strconv" "strings" "time" ) func producer(intChan chan int) { for b := 1000; b <= 1099; b++ { intChan <- 阅读全文
posted @ 2023-04-24 19:24 ascertain 阅读(13) 评论(0) 推荐(0) 编辑
摘要:slice, map即使为nil打印格式和空元素的情况一样, chan,func,interface,pointer为nil时,打印<nil> 当chan,func,pointer赋值后,都打印地址, interface赋值后,永远打印其dynamic value的格式 package main i 阅读全文
posted @ 2023-04-18 16:47 ascertain 阅读(12) 评论(0) 推荐(0) 编辑
摘要:replace module: module-b module: module-a 阅读全文
posted @ 2023-03-29 23:56 ascertain 阅读(10) 评论(0) 推荐(0) 编辑
摘要:1. send-only chan<- & receive-only <-chan channel package main import ( "fmt" "time" ) func producer(ch chan<- int, exitChan chan struct{}) { for i := 阅读全文
posted @ 2023-03-18 00:00 ascertain 阅读(10) 评论(0) 推荐(0) 编辑
摘要:func GetGoroutineID() int { var buf = make([]byte, 128) b := runtime.Stack(buf, false) idField := strings.Fields(strings.TrimPrefix(string(buf[:b]), " 阅读全文
posted @ 2023-03-17 20:43 ascertain 阅读(15) 评论(0) 推荐(0) 编辑
摘要:1. channel func main() { boolChan := make(chan bool, 100) for i := 0; i < 100; i++ { go func(v int) { println(v) boolChan <- true }(i) } for i := 0; i 阅读全文
posted @ 2023-03-17 20:42 ascertain 阅读(9) 评论(0) 推荐(0) 编辑
摘要:env: GO111MODULE go env -w GO111MODULE=on GOPROXY go env -w GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/goproxy,direct GONOPROXY, GONOSUMDB, 阅读全文
posted @ 2023-02-07 21:51 ascertain 阅读(39) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" ) type node struct { id int name string next *node // Node会造成循环引用 } type SingleLinkedList struct { head *node // head!=nil 阅读全文
posted @ 2022-05-23 20:22 ascertain 阅读(30) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" ) type Node struct { row int col int val int } func main() { var vail [7][9]int vail[2][8] = 5 vail[5][3] = 6 for _, row : 阅读全文
posted @ 2022-05-23 16:03 ascertain 阅读(20) 评论(0) 推荐(0) 编辑
摘要:package main import ( "errors" "fmt" "os" ) type Queue struct { size int array []int head int tail int } func (queue *Queue) Push(v int) error { if qu 阅读全文
posted @ 2022-05-23 15:10 ascertain 阅读(23) 评论(0) 推荐(0) 编辑
摘要:https://github.com/go-redis/redis package main import ( "context" "fmt" "time" "github.com/go-redis/redis/v8" ) var ( ctx context.Context rdb *redis.C 阅读全文
posted @ 2022-05-20 17:47 ascertain 阅读(29) 评论(0) 推荐(0) 编辑
摘要:Server: package main import ( "fmt" "net" ) func main() { fmt.Println("net.Listen") listen, err := net.Listen("tcp", "0.0.0.0:5555") if err != nil { f 阅读全文
posted @ 2022-05-20 16:51 ascertain 阅读(32) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "reflect" ) type Vale struct { Name string `json:"name,omitempty"` Age int `json:"vale_age,omitempty"` Score float32 `json 阅读全文
posted @ 2022-05-20 15:31 ascertain 阅读(24) 评论(0) 推荐(0) 编辑
摘要:https://stackoverflow.com/questions/31650192/whats-the-full-name-for-iota-in-golang 一个常量块中,一个常量没有赋值,取上个常量的值 iota从0开始,每行++ 不推荐使用iota 阅读全文
posted @ 2022-05-20 12:12 ascertain 阅读(15) 评论(0) 推荐(0) 编辑
摘要:func TypeAssert(items ...interface{}) { for i, v := range items { switch v.(type) { case bool: fmt.Printf("index: %d, type: %T, value: %v\n", i, v, v) 阅读全文
posted @ 2022-05-19 23:02 ascertain 阅读(17) 评论(0) 推荐(0) 编辑
摘要:channel不能使用for循环动态遍历 package main import ( "fmt" ) func main() { intChan := make(chan int, 10) for b := 0; b < cap(intChan); b++ { intChan <- b + 100 阅读全文
posted @ 2022-05-19 22:48 ascertain 阅读(21) 评论(0) 推荐(0) 编辑
摘要:func GetGoroutineId() (n uint64) { vail := make([]byte, 1024) vail = vail[:runtime.Stack(vail, true)] fmt.Println(string(vail)) vail = bytes.TrimPrefi 阅读全文
posted @ 2022-05-19 20:02 ascertain 阅读(22) 评论(0) 推荐(0) 编辑
摘要:Polymorphic Array: package main import ( "fmt" ) type USB interface { Start() Stop() } type Phone struct { Name string } func (p *Phone) Start() { fmt 阅读全文
posted @ 2022-05-18 18:40 ascertain 阅读(18) 评论(0) 推荐(0) 编辑
摘要:package main import ( "fmt" "math/rand" "sort" "time" ) // Vale Vale结构体 type Vale struct { Name string Age int } func (vale Vale) String() string { re 阅读全文
posted @ 2022-05-18 18:22 ascertain 阅读(20) 评论(0) 推荐(0) 编辑
摘要:结构体的接口实现方法参数为指针类型时,会报编译错误 package main type USB interface { method() } type Vale struct { } func (vale *Vale) method() { // 指针类型 println("vale method" 阅读全文
posted @ 2022-05-18 16:40 ascertain 阅读(16) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示