Https://gobyexample.com/goroutines doesn't always work


(larry) #1

https://gobyexample.com/goroutines

I’ve been trying to learn go with this but I ran the code on the playground:

direct : 0
direct : 1
direct : 2
done

Is a go like a fork in C?

I’m guessing it just finished before the go functions could process.

I added a print and change the results

fmt.Println(“test”)
before-- go f(“goroutine”)

direct : 0
direct : 1
direct : 2
test
going
goroutine : 0
goroutine : 1
goroutine : 2
done

Might want to explain the results shown on the example page might not always be that


(George Calianu) #2

Well, concurency is an advanced concept, do not start learning Go with this. Try some simple things to understand the concepts. Related to your problem concurency have some rules. Being something like a little process in your program is scheduled by the runtime and often you can see that the goroutine finish, the next instruction is executed and after that the goroutine do something. That’s normal in concurency and you will see many things executed in unexpected order. To do things in order you must synchronize the goroutines by hand.

Suggested book for entry level:
http://www.golang-book.com/books/intro


(larry) #3

I’m not just starting learning concurrency, but I’m not an expert either. I’ve dealt with multi-theaded programs in the past and written some but it’s been years. That’s why I was asking if “go” was similar to to “fork” in C.

I also thought, since it’s a learning site, you might want to explain that results can vary from the stated.


(George Calianu) #4

Go sintax is closer to C indeed.

you might want to explain that results can vary from the stated

In concurency the routines start at various moments in time and take a time to initialize and do some action. In this time the program continue and some time later you could see the result of the goroutine. There is no default or guaranteed order in concurency, so if you launch a few goroutines in some order the results may not respect that order. Also concurency is not parralelism.


(Jay Ts) #5

Hi Larry,

I agree with what George wrote.

I also had prior experience with multiprocessing with the fork() system call, and multi-threading with POSIX threads. Those helped me understand many of the fundamental concepts of concurrency. So I think you have a very good place to start from.

Concurrency in Go is something more than those, so you will need to learn about how concurrency is implemented in Go, along with channels, which are used for inter-goroutine communication, including data transfer and signaling.

There are many good learning resources, including chapters of various books on Go, a whole book, Concurrency in Go, lessons in video courses. (Ultimate Go by @William Kennedy is excellent IMO), and also videos on YouTube. After you understand the fundamentals, try searching for videos on Go concurrency patterns.


(larry) #6

Thank You guys. I’ll work on it. I was trying to get the basics by going through gobyexample. I’ll be trying to write some code after that. I would love to take time to learn slowly but I’ll probably have to fumble through some. Most of the stuff I’ll be trying to begin with won’t be anything complex.