You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
https://sirius1y.top/posts/notes/go-srccode/go-channel/
channel的简单使用 在Go语言中,通道(channel)是一种用于在goroutine之间进行通信和同步的机制。下面是一些简单的通道使用示例,以及它们对应的底层函数调用。
package main import ( "fmt" "time" ) func main() { ch := make(chan int) // 创建通道 go func() { ch <- 42 // 发送数据到通道 }() go func() { value := <-ch // 从通道接收数据 fmt.Println("Received:", value) }() time.Sleep(1 * time.Second) // 等待goroutine完成 close(ch) // 关闭通道 } 底层函数调用 创建通道:
ch := make(chan int) 底层调用:
makechan(elemtype, size) 发送数据到通道:
ch <- 42 底层调用:
chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool 从通道接收数据:
https://sirius1y.top/posts/notes/go-srccode/go-channel/
Beta Was this translation helpful? Give feedback.
All reactions