feat(guides): add example to goroutines (#20695)

This commit is contained in:
Nadir Fayazov
2018-11-03 17:23:13 -07:00
committed by Christopher McCormack
parent a3518d677d
commit e48814fd15

View File

@ -3,16 +3,32 @@ title: Goroutines
---
## Goroutines
This is a stub. [Help our community expand it](https://github.com/freecodecamp/guides/tree/master/src/pages/go/goroutines/index.md).
[This quick style guide will help ensure your pull request gets accepted](https://github.com/freecodecamp/guides/blob/master/README.md).
<!-- The article goes here, in GitHub-flavored Markdown. Feel free to add YouTube videos, images, and CodePen/JSBin embeds -->
Goroutines are functions or methods that run concurrently with other functions or methods. Goroutines can be thought of as light weight threads. The cost of creating a Goroutine is tiny when compared to a thread.
Prefix the function or method call with the keyword `go` and you will have a new Goroutine running concurrently.
Let's look at an example:
func say(s string) {
fmt.Println(s)
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
func main() {
go say("hello")
say("world")
}
Output:
world
hello
hello
world
You can observe that since we called `say("hello")` in a goroutine, it'll run concurrently and will print the output in no particular order in regards to the regualar function call `say("world")`.
#### More Information:
* [A Tour of Go](https://tour.golang.org/concurrency/1)
* [Go By Example](https://gobyexample.com/goroutines)