03 2021 档案

摘要:WithCancel(主进程控制子协程关闭) package main import ( "context" "fmt" "sync" "time" ) var wg sync.WaitGroup func f(ctx context.Context) { defer wg.Done() LOOP: 阅读全文
posted @ 2021-03-06 11:59 GPHPER 阅读(170) 评论(0) 推荐(0)
摘要:sync.WaitGroup 优雅的同步执行协程 package main import ( "fmt" "math/rand" "sync" "time" ) func f1(i int) { defer wg.Done() rand.Seed(time.Now().UnixNano()) tim 阅读全文
posted @ 2021-03-05 21:24 GPHPER 阅读(174) 评论(0) 推荐(0)
摘要:select就是用来监听和channel有关的IO操作,当 IO 操作发生时,触发相应的动作 package main import ( "fmt" "time" ) func main() { ch := make(chan int) o := make(chan bool) go func() 阅读全文
posted @ 2021-03-05 20:57 GPHPER 阅读(86) 评论(0) 推荐(0)
摘要:Timer定时器 启动 package main import ( "fmt" "time" ) func main() { <-time.After(2 * time.Second) fmt.Println("延时两秒") } func main02() { time.Sleep(2 * time 阅读全文
posted @ 2021-03-03 21:15 GPHPER 阅读(128) 评论(0) 推荐(0)
摘要:Channel通道 无缓存通道 make(chan type类型) 注意:读和写都是阻塞执行的 package main import ( "fmt" "time" ) var ch = make(chan int) func Printer(str string) { for _, data := 阅读全文
posted @ 2021-03-02 21:38 GPHPER 阅读(95) 评论(0) 推荐(0)
摘要:当主协程退出后子协程也会退出 package main import ( "fmt" "time" ) func main() { go func() { i := 0 for { i++ fmt.Println("son i = ", i) time.Sleep(time.Second) } }( 阅读全文
posted @ 2021-03-02 20:57 GPHPER 阅读(140) 评论(0) 推荐(0)
摘要:标准设备文件操作 package main import ( "os" ) func main() { os.Stdout.WriteString("hello world") //相当于fmt.Println } 磁盘文件操作 package main import ( "bufio" "fmt" 阅读全文
posted @ 2021-03-02 20:24 GPHPER 阅读(118) 评论(0) 推荐(0)

TOP