随笔分类 - Go
摘要:package main import ( "fmt" "math" "runtime" "strconv" "strings" "time" ) func producer(intChan chan int) { for b := 1000; b <= 1099; b++ { intChan <-
阅读全文
摘要:slice, map即使为nil打印格式和空元素的情况一样, chan,func,interface,pointer为nil时,打印<nil> 当chan,func,pointer赋值后,都打印地址, interface赋值后,永远打印其dynamic value的格式 package main i
阅读全文
摘要:replace module: module-b module: module-a
阅读全文
摘要:1. send-only chan<- & receive-only <-chan channel package main import ( "fmt" "time" ) func producer(ch chan<- int, exitChan chan struct{}) { for i :=
阅读全文
摘要:func GetGoroutineID() int { var buf = make([]byte, 128) b := runtime.Stack(buf, false) idField := strings.Fields(strings.TrimPrefix(string(buf[:b]), "
阅读全文
摘要: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
阅读全文
摘要:env: GO111MODULE go env -w GO111MODULE=on GOPROXY go env -w GOPROXY=https://goproxy.cn,https://mirrors.aliyun.com/goproxy,direct GONOPROXY, GONOSUMDB,
阅读全文
摘要:package main import ( "fmt" ) type node struct { id int name string next *node // Node会造成循环引用 } type SingleLinkedList struct { head *node // head!=nil
阅读全文
摘要: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 :
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要: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
阅读全文
摘要:package main import ( "fmt" "reflect" ) type Vale struct { Name string `json:"name,omitempty"` Age int `json:"vale_age,omitempty"` Score float32 `json
阅读全文
摘要:https://stackoverflow.com/questions/31650192/whats-the-full-name-for-iota-in-golang 一个常量块中,一个常量没有赋值,取上个常量的值 iota从0开始,每行++ 不推荐使用iota
阅读全文
摘要: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)
阅读全文
摘要:channel不能使用for循环动态遍历 package main import ( "fmt" ) func main() { intChan := make(chan int, 10) for b := 0; b < cap(intChan); b++ { intChan <- b + 100
阅读全文
摘要:func GetGoroutineId() (n uint64) { vail := make([]byte, 1024) vail = vail[:runtime.Stack(vail, true)] fmt.Println(string(vail)) vail = bytes.TrimPrefi
阅读全文
摘要:Polymorphic Array: package main import ( "fmt" ) type USB interface { Start() Stop() } type Phone struct { Name string } func (p *Phone) Start() { fmt
阅读全文
摘要:package main import ( "fmt" "math/rand" "sort" "time" ) // Vale Vale结构体 type Vale struct { Name string Age int } func (vale Vale) String() string { re
阅读全文
摘要:结构体的接口实现方法参数为指针类型时,会报编译错误 package main type USB interface { method() } type Vale struct { } func (vale *Vale) method() { // 指针类型 println("vale method"
阅读全文