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.
1package main
2
3import (
4 "fmt"
5 "runtime"
6)
7
8func print(till int, message string) {
9 for i := 0; i < till; i++ {
10 fmt.Println((i + 1), message)
11 }
12}
13func main() {
14 runtime.GOMAXPROCS(2)
15 go print(5, "hello")
16 print(5, "how are you")
17 var input string
18 fmt.Scanln(&input)
19}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.
1➜ simple git:(main) ✗ go run main.go
21 what's up
32 what's up
43 what's up
54 what's up
65 what's up
71 hello
82 hello
93 hello
104 hello
115 hello
12
13➜ simple git:(main) ✗ go run main.go
141 halo
152 halo
163 halo
174 hello
185 hello
191 how are you
202 how are you
213 how are you
224 how are you
235 how are youThe 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.
1func 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.
1var s1, s2, s3 strings
2fmt.Scanln(&s1, &s2, &s3)
3
4// user inputs: "ihsan san tekno"
5
6fmt.Println(s1) // ihsan
7fmt.Println(s2) // san
8fmt.Println(s3) // teknoAs 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.