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!
*/
标签:great,printMessage,Channels,go,Go,main,Rust,GoRoutines From: https://www.cnblogs.com/Answer1215/p/18009906