I think this is feature which you’re very interested in this programming language. If you knew thread in Java, Python, …; in Golang A goroutine is a lightweight thread of execution.

Suppose we have a function call bigLoop(s), this function should loop by for statement 1000 times and print i.

func bigLoop(from string) {
    for i := 0; i < 1000; i++ {
        fmt.Println(from, ":", i)

Here’s how we’d call that in the usual way, running it synchronously.

To invoke this function in a goroutine, use go f(s). This new goroutine will execute concurrently with the calling one. That mean bigLoop func will process in a standalone thread, our two function calls are running asynchronously in separate goroutines now. So, you can do anything after call goroutine and program are still running  falls through to Scanln requires we press a key before the program exits.

func main() {


    go bigLoop("Invoke by goroutine:")



When we run this program, we see the output of the blocking call first, then the interleaved output of the two goroutines. This interleaving reflects the goroutines being run concurrently by the Go runtime.

In a program of Golang, if we have a lot of goroutines to control, how to communicate along many goroutines. Using channel in next article

View on github