[ 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)

posted @   Zhentiw  阅读(1)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源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
点击右上角即可分享
微信分享提示