[ Go] GoRoutines and Channels
A goroutine is the Go way of suing threads, we open a goroutine just by invoking any function with a go
prefix.
go functionCall()
Goroutines can communicate through channels, an special type of variable, a channel contains a value of any kind, a routine can define a value for a channel and other routine can wait for that value.
Channels can be buffered or not.
// define a chan type is string
var foo chan string
// constructor make
bar := make(chan string)
// write into channel
bar <- "hello"
// other can read message
message := <- bar
Goroutines example:
// Without go routines
package main
import (
"fmt"
"time"
)
func printMessage(text string) {
for i := 0; i < 3; i++ {
fmt.Println(text)
time.Sleep(300 * time.Millisecond)
}
}
func main() {
printMessage("Go is great!")
printMessage("Rust is great!")
}
Output:
/*
Go is great!
Go is great!
Go is great!
Rust is great!
Rust is great!
Rust is great!
/*
Without Goroutines
func main() {
go printMessage("Go is great!")
printMessage("Rust is great!")
}
Output:
/*
Rust is great!
Go is great!
Go is great!
Rust is great!
Rust is great!
Go is great!
/*
Notice that you cannot do
func main() {
go printMessage("Go is great!")
go printMessage("Rust is great!")
}
No output for this code, because the main goroutines ends when main function exit, which also ends sub goroutines.
Channel
package main
import (
"fmt"
"time"
)
func printMessage(text string, chanMsg chan string) {
for i := 0; i < 3; i++ {
fmt.Println(text)
time.Sleep(300 * time.Millisecond)
}
chanMsg <- "Done!"
}
func main() {
chanMsg := make(chan string)
go printMessage("Go is great!", chanMsg)
// Wait channel message then print
response := <-chanMsg
fmt.Println(response)
}
/*
Go is great!
Go is great!
Go is great!
DONE!
*/
It is also possible to use buffer, for example we want to buffer size 2:
func printMessage(text string, chanMsg chan string) {
for i := 0; i < 3; i++ {
fmt.Println(text)
time.Sleep(300 * time.Millisecond)
}
chanMsg <- "Done!"
chanMsg <- "Done!2"
// chanMsg <- "Done!"
}
func main() {
chanMsg := make(chan string, 2)
go printMessage("Go is great!", chanMsg)
// Wait channel message then print
response := <-chanMsg
response2 := <-chanMsg
fmt.Println(response)
fmt.Println(response2)
}
/*
Go is great!
Go is great!
Go is great!
Done!
Done!2
*/
To avoid deadlocks you have to close the channels before ending the program with close(chan)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2023-02-06 [Typescript] Global Scope
2023-02-06 [Typescript] Indexing an Object with Branded Types
2019-02-06 [TypeScript] Type Definitions and Modules
2019-02-06 [Tools] Add a Dynamic Tweet Button to a Webpage
2019-02-06 [Algorithm] Find Nth smallest value from Array
2018-02-06 [Javascript] Delegate JavaScript (ES6) generator iteration control
2017-02-06 [React] Use React.cloneElement to Extend Functionality of Children Components