Questions tagged [goroutine]

A goroutine is a lightweight thread of execution that is managed by the Go language runtime.

A goroutine is a lightweight thread of execution that is fully managed by the Go language runtime. The creators of Go felt that existing terms (e.g. threads, coroutines, processes) would incorrectly suggest exactly what a goroutine is, so they coined the new word.

The go keyword is used to execute a function in a new goroutine:

go f(x, y)

f, x, and y are evaluated in the current goroutine, and then a new goroutine is started where f executes.

A goroutine runs in the same address space as the goroutine that started it. Goroutines may or may not be run in a different OS thread. Most often, goroutines communicate through channels.

More Information:

See Also:

1673 questions
171
votes
12 answers

How to asynchronously call a method in Java

I've been looking at Go's goroutines lately and thought it would be nice to have something similar in Java. As far as I've searched the common way to parallelize a method call is to do something like: final String x = "somethingelse"; new Thread(new…
Felipe Hummel
  • 4,674
  • 5
  • 32
  • 35
168
votes
5 answers

How to wait for all goroutines to finish without using time.Sleep?

This code selects all xml files in the same folder, as the invoked executable and asynchronously applies processing to each result in the callback method (in the example below, just the name of the file is printed out). How do I avoid using the…
Dante
  • 10,722
  • 16
  • 51
  • 63
154
votes
8 answers

How to stop a goroutine

I have a goroutine that calls a method, and passes returned value on a channel: ch := make(chan int, 100) go func(){ for { ch <- do_stuff() } }() How do I stop such a goroutine?
Łukasz Gruner
  • 2,929
  • 3
  • 26
  • 28
136
votes
5 answers

Catching return values from goroutines

The below code gives compilation error saying 'unexpected go': x := go doSomething(arg) func doSomething(arg int) int{ ... return my_int_value } I know, I can fetch the return value if I call the function normally i.e. without using…
Nerve
  • 6,463
  • 4
  • 29
  • 29
115
votes
3 answers

Example for sync.WaitGroup correct?

Is this example usage of sync.WaitGroup correct? It gives the expected result, but I am unsure about the wg.Add(4) and the position of wg.Done(). Does it make sense to add the four goroutines at once with…
topskip
  • 16,207
  • 15
  • 67
  • 99
115
votes
7 answers

Multiple goroutines listening on one channel

I have multiple goroutines trying to receive on the same channel simultaneously. It seems like the last goroutine that starts receiving on the channel gets the value. Is this somewhere in the language spec or is it undefined behaviour? c :=…
Ilia Choly
  • 18,070
  • 14
  • 92
  • 160
103
votes
7 answers

Max number of goroutines

How many goroutines can I use painless? For example wikipedia says, in Erlang 20 million processes can be created without degrading performance. Update: I've just investigated in goroutines performance a little and got such a results: It looks like…
OCyril
  • 2,875
  • 4
  • 20
  • 11
81
votes
6 answers

Is a Go goroutine a coroutine?

In the Google I/O 2012 presentation Go Concurrency Patterns, Rob Pike mentions that several goroutines can live in one thread. Does this imply that they are implemented as coroutines? If not, how they are implemented? Links to source code would be…
Sławosz
  • 11,187
  • 15
  • 73
  • 106
78
votes
8 answers

Always have x number of goroutines running at any time

I see lots of tutorials and examples on how to make Go wait for x number of goroutines to finish, but what I'm trying to do is have ensure there are always x number running, so a new goroutine is launched as soon as one ends. Specifically I have a…
Alasdair
  • 13,348
  • 18
  • 82
  • 138
78
votes
7 answers

How do goroutines work? (or: goroutines and OS threads relation)

How can other goroutines keep executing whilst invoking a syscall? (when using GOMAXPROCS=1) As far as I'm aware of, when invoking a syscall the thread gives up control until the syscall returns. How can Go achieve this concurrency without creating…
omribahumi
  • 2,011
  • 1
  • 17
  • 19
75
votes
7 answers

context.WithValue: how to add several key-value pairs

With Go's context package it is possible to pass request-specific data to the stack of request handling functions using func WithValue(parent Context, key, val interface{}) Context This creates a new Context which is a copy of parent and contains…
alex
  • 2,252
  • 4
  • 23
  • 34
71
votes
5 answers

How would you define a pool of goroutines to be executed at once?

TL;DR: Please just go to the last part and tell me how you would solve this problem. I've begun using Go this morning coming from Python. I want to call a closed-source executable from Go several times, with a bit of concurrency, with different…
user1940040
70
votes
6 answers

How best do I keep a long running Go program, running?

I've a long running server written in Go. Main fires off several goroutines where the logic of the program executes. After that main does nothing useful. Once main exits, the program will quit. The method I am using right now to keep the program…
Nate
  • 5,237
  • 7
  • 42
  • 52
64
votes
3 answers

What is the difference between switch and select in Go?

Is there any difference between switch and select in Go, apart from the fact that one takes an argument and the other not?
Thomas
  • 8,306
  • 8
  • 53
  • 92
61
votes
7 answers

Explain: Don't communicate by sharing memory; share memory by communicating

I wonder what is the most down to earth explanation of this famous quote: Don't communicate by sharing memory; share memory by communicating. (R. Pike) In The Go Memory Model I can read this: A send on a channel happens before the corresponding…
honzajde
  • 2,270
  • 3
  • 31
  • 36
1
2 3
99 100