Golang Getting Started Series 6: understand Goroutine

The previous talk is about some basic knowledge of the Go language. If you are interested, you can take a look at the previous article. http://quickintro123.com/index.php/category/golang/

Today, let's talk about the advanced functions in go, which is also an important feature of the go language: Goroutine.

What is a Goroutine?

A Goroutine is a function that runs simultaneously with other functions. You can think of Goroutine as lightweight threads that are managed by the Go runtime.

Add the go keyword before the function call, and this call will be executed concurrently in a new goroutine. When the called function returns, this goroutine also ends automatically. It sounds like Task in C#.

It should be noted that if this function has a return value, the return value will be discarded.

Goroutine communicate through channels, which is simply a communication channel between multiple goroutines. Channels can prevent resource contention when multiple goroutines access shared memory.

How to use

package main

import (  
    "fmt"
    "time"
)

func hello() {  
    fmt.Println("Hello world goroutine")
}
func main() {  
    go hello()
    time.Sleep(1 * time.Second)
    fmt.Println("main function")
}

Channel

Channel can be thought of as a channel of communication between goroutines. Just as water flows from one end of the pipe to the other, data can be sent from one end of the channel and received at the other.

1. Definitions

Each channel has a type. This type is the type of data that the channel is allowed to transmit. Channels are type-dependent. A channel can only pass one type of value. This type needs to be specified when the channel is declared.

2. Declarations

a. We need to create a channel through the built-in function make.

The following code declares a channel:

var ch chan int

b. As with other variable definitions, quick declarations are an effective and concise way to define channels:

a := make(chan int) 

c. Create a buffered channel

c := make(chan int, 1024)

// Read data from buffered channel
for i:=range c {
  ...
}

3. Send and receive data

The syntax for sending and receiving data over a channel is as follows:

data := <- a   // read data from channel a
a <- data      // write data to channel a  

The direction of the arrow indicates whether the data is sent or received.

Complete example

Let‘s talk directly about the complete example of Goroutine and channel used together:

Package code

import (
    "fmt"
    "time"
)

func Producer(queue chan<- int) {
    for i := 0; i < 10; i++ {
        queue <- i // Write
        fmt.Println("create :", i)
    }
}

func Consumer(queue <-chan int) {
    for i := 0; i < 10; i++ {
        v := <-queue  // Read out
        fmt.Println("receive:", v)
    }
}

func main() {
    queue := make(chan int, 88)
    go Producer(queue)
    go Consumer(queue)
    time.Sleep(1 * time.Second)
}

At last

Above, I have briefly introduced the goroutines and channels in Go. These two features are important features in the Go language. You can write other examples and get familiar with them.

This entry was posted in development, golang and tagged , , . Bookmark the permalink.

One Response to Golang Getting Started Series 6: understand Goroutine

  1. Like!! Great article post.Really thank you! Really Cool.

Leave a Reply

Your email address will not be published. Required fields are marked *