Goroutines
Goroutines are functions and methods that run concurrently in a Go program.
Although goroutines share some similarities with threads, there are important differences that include the following:
- Threads depend on the hardware of the host computer’s operating system, whereas goroutines do not.
- Compared with threads, goroutines are cost-effective and use fewer resources to function.
- Goroutines do not use thread-local storage and, thus, do not have a unique ID.
Comparatively, goroutines are light-weight versions of threads that operate within the context of the Go runtime.
Syntax
func myFunction(parameter) returnType {
// function body
}
go myFunction()
A goroutine is started by invoking a previously defined function or method, myFunction()
, with the go
keyword.
Note: For any goroutines to run, the
main()
function must be defined and executed. When themain()
function terminates, the program will be terminated, and no other goroutine will run.
Example
In the following example, a goroutine is defined to print out text. Inside the main()
function, myGoroutine()
is called with the go
keyword and causes a time delay of 2 seconds with time.Sleep()
before printing the text:
package mainimport ("fmt""time")func myGoroutine() {fmt.Println("This is my first goroutine")}func main() {go myGoroutine()time.Sleep(2 * time.Second)fmt.Println("This is the main function")}
This results in the following output:
This is my first goroutineThis is the main function
Note: When a goroutine is started, the goroutine call returns immediately. The program does not wait for the goroutine to finish executing. After the goroutine call, the program continues the next line of code, and any return values from the goroutine are ignored. The
time.Sleep()
function keeps themain()
function running while themyGoroutine()
function executes. Without the two seconds waiting time in thetime.Sleep()
function, the second print statement could be immediately completed, and themain()
function could terminate before the goroutine is completed.
Codebyte Example
The example below defines a print()
function to print out a string, which is first called as a goroutine and then as a regular function:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.