I am studying about golang.
in golang, there is a concept, called goroutine. i write a sample code.
package main
import (
"fmt"
"runtime"
)
func main() {
runtime.GOMAXPROCS(1) // cpu core를 하나만 사용하도록 해 놓음
s := "test string"
for i := 0; i < 100; i++ {
go func(n int) {
fmt.Println(s, n)
}(i)
}
fmt.Scanln()
}
if you interpret code and run, you will see first printing data is
test string 99
I don't know why result it is. Anyone help me?
It is because of this line:
As the goroutine are a way to execute code concurrently, all the goroutine you create in your loop have to wait for a proc.
As you have just one, and it is busy executing the loop, you're going to have a stack of goroutine waiting.
When the loops ends the proc have time to execute the first goroutine, that is the last created, and then do the same thing whith the stack one by one, but this time in sequence.
Try to put 2 procs and look what will happen.
Keep in mind that when you work in concurrent the order in which the code is executed is not guarantee, as already has been mentioned in a comment.
Here you have a sequence, just because you have just one process.
If your purpose is to have items processed sequencially even in a concurrent way, you should use channels.
Let me show a sample of code that do the job:
package main
As you can see is just a new version of your, but if you execute you should get the proper sequence.
The main changes are that you have 2 goroutine now, one for the print and one for the loop.
The 2 goroutine talk throungh a channel of int.
When the program start the first goroutin call the function printNumbers, that stai waiting until something is written into the channel.
Then start the second gorouting that write one by one the integers into the channel.
Once the for is ended, as you have just 1 process, the printNumbers function start again as there are items inside the channel.
It loops until it end the itmes.
Of course, you have 2 loops here, but you can't avoid it.
So now try to ass process as you wish and look what happen.