I don't understand Goroutines and Channels?

I come from JS and I never heard the word Concurrency before…
I am lost because I don’t understand Concurrency, I don’t know when I have to use it too.
I have already looked at golang tour and go by examples but…

I use go to build HTTP Server,
Can anyone explain me please ?

Work through the Golang Tour, especially the part that covers Concurrency.

Also watch the talk Go Concurrency Patterns (slides).

1 Like

I didn’t see the talk but I already saw the Golang tour Concurrency section… And I don’t understand why and when I have to use it…

Then watch the talk, please.

1 Like

See here what concurrency is and also read why concurency is not parallelism.

1 Like

I saw the entire Talk and my conclusion is : I don’t need Concurrency to build http servers… Is it right ?

Thanks for the wiki link :blush: I’ve already seen the difference between parallelism and concurrency

Depends on your load… .

No concurrency on a Webserver means that you can not handle a request before previous requests have been finished.

1 Like

I didn’t think about it… So for each user’s request, the next user has to wait for the previous request ?

Yes. This is nothing different in Node, Ruby, PHP.

When you do not do concurrency, then you can not do 2 things at a time.

But to be honest, when you just want to create a website using Go, you do not need to worry about concurrency and channels yet.

All you need to know is, that you can handle the request in your handler, and dealing with many requests concurrently is abstracted away from you.

As long as your database is the only source of shared data and you do not use shared variables, you won’t even notice that you are handling the requests concurrently.

1 Like

Thanks for your explanation :smirk:

Be patient, code what you can and you will learn more and more :blush:


Last question… Are goroutines like promises in JavaScript ?

No, but if you are comming from JS world, goroutines allow similar thing but are much more powerfull.

Yes I come from JS… I used NodeJS but now I use go with mux… If you can do the same thing, all my requests to API’s can use goroutines ?

All go http handler methods are already goroutines, so you don’t have to worry about multiple requests. You maybe need your goroutines in some cases, but you can start without.

1 Like

Thanx but, another question : I wanted to know when should I use the goroutines ?

For example you want to scrape some websites in parallel or ferch some RSS, this is first that came to my mind but there are so many possibilities. Simply, when you want something to be done concurrently or parallel.

Like asynchronous in JS ?

Yes, but without callbacks mess.

1 Like