programming

Introduction of Go Routine on Golang

Use go-routine when the process to be executed as a goroutine must be wrapped in a function. When calling the function, the go command is added in front of it. Thus the process will be detected as a new goroutine.

Here is a simple example of a simple goroutine implementation. The program displays 10 lines of text, of which 5 lines are printed using a regular function and the other 5 are printed using a new goroutine.

package main

import (
	"fmt"
	"runtime"
)

func print(till int, message string) {
	for i := 0; i < till; i++ {
		fmt.Println((i + 1), message)
	}
}
func main() {
	runtime.GOMAXPROCS(2)
	go print(5, "hello")
	print(5, "how are you")
	var input string
	fmt.Scanln(&input)
}

In the above code, runtime.GOMAXPROCS(2) is used to determine the number of active processors.

The creation of a new goroutine is marked with go. For example in the command go print(5, "hello"), the function print() is executed as a new goroutine. The fmt.Scanln() function is used so that the application running process stops at that line blockig until the user presses the enter key.

This is necessary because it is possible that the print() goroutine takes longer to execute than the completion time of the main main() goroutine, since both processes are executed asynchronously. If we don’t use the command, the unfinished goroutine is forcibly stopped because the main goroutine has already finished executing.

➜ simple git:(main) ✗ go run main.go
1 what's up
2 what's up
3 what's up
4 what's up
5 what's up
1 hello
2 hello
3 hello
4 hello
5 hello

➜ simple git:(main) ✗ go run main.go
1 halo
2 halo
3 halo
4 hello
5 hello
1 how are you
2 how are you
3 how are you
4 how are you
5 how are you

The words “hello” and “how are you” appear alternately because the statement print(5, "hello") is executed with a new goroutine, making it not wait for each other with the command `print(5, “how are you”).

In the results of executing the above program twice. The first execution result is different from the second because we use 2 processors. Which goroutine will be executed first depends on the two processors.

Use of Function runtime.GOMAXPROCS()

This function is used to determine the number of processors used in program execution.

The number of processors inputted will automatically be adjusted to the original number of logical processors on the server or computer. If the inputted number is more, it is considered to use all available processors.

Function Usage fmt.Scanln()

This function will capture all the characters before the user presses the enter key, and then store them in a variable.

func Scanln(a ...interface{}) (n int, err error)

The above function is a schematic of the fmt.Scanln() function. The function can hold an unlimited number of parameters of type interface{}. Each parameter will be accommodated in a variable separated by a space mark. For clarity, please see the example below.

var s1, s2, s3 strings
fmt.Scanln(&s1, &s2, &s3)

// user inputs: "ihsan san tekno"

fmt.Println(s1) // ihsan
fmt.Println(s2) // san
fmt.Println(s3) // tekno

As can be seen in the code above, to hold the parameters ihsan san tekno we need 3 variables. It is also worth noting that what is inserted as a parameter in the fmt.Scanln() function call is the variable reference, not the original value.

comments powered by Disqus