Skip to content

Commit 2ed6d04

Browse files
cslyllaSSwiniarskiDusch4593
authored
New entry go goroutines (#1410)
* Create folder, file and template * Draft content and add syntax * Draft content * Add example * Review draft * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: SSwiniarski <[email protected]> * Remove header * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Update content/go/concepts/goroutines/goroutines.md Co-authored-by: Brandon Dusch <[email protected]> * Minor wording updates * Update goroutines.md Co-authored-by: SSwiniarski <[email protected]> Co-authored-by: Brandon Dusch <[email protected]>
1 parent 361d8bf commit 2ed6d04

File tree

1 file changed

+97
-0
lines changed

1 file changed

+97
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
Title: 'Goroutines'
3+
Description: 'Goroutines are functions and methods that run concurrently with other functions and methods.'
4+
Subjects:
5+
- 'Code Foundations'
6+
- 'Computer Science'
7+
Tags:
8+
- 'Control Flow'
9+
- 'Threads'
10+
- 'Concurrency'
11+
- 'Functions'
12+
CatalogContent:
13+
- 'learn-go'
14+
- 'paths/back-end-engineer-career-path'
15+
---
16+
17+
**Goroutines** are functions and methods that run concurrently in a Go program.
18+
19+
Although goroutines share some similarities with threads, there are important differences that include the following:
20+
21+
- Threads depend on the hardware of the host computer's operating system, whereas goroutines do not.
22+
- Compared with threads, goroutines are cost-effective and use fewer resources to function.
23+
- Goroutines do not use thread-local storage and, thus, do not have a unique ID.
24+
25+
Comparatively, goroutines are light-weight versions of threads that operate within the context of the Go runtime.
26+
27+
## Syntax
28+
29+
```pseudo
30+
func myFunction(parameter) returnType {
31+
// function body
32+
}
33+
34+
go myFunction()
35+
```
36+
37+
A goroutine is started by invoking a previously defined function or method, `myFunction()`, with the `go` keyword.
38+
39+
> **Note:** For any goroutines to run, the `main()` function must be defined and executed. When the `main()` function terminates, the program will be terminated, and no other goroutine will run.
40+
41+
## Example
42+
43+
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:
44+
45+
```go
46+
package main
47+
import (
48+
"fmt"
49+
"time"
50+
)
51+
52+
func myGoroutine() {
53+
fmt.Println("This is my first goroutine")
54+
}
55+
56+
func main() {
57+
go myGoroutine()
58+
time.Sleep(2 * time.Second)
59+
fmt.Println("This is the main function")
60+
}
61+
```
62+
63+
This results in the following output:
64+
65+
```shell
66+
This is my first goroutine
67+
This is the main function
68+
```
69+
70+
> **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 the `main()` function running while the `myGoroutine()` function executes. Without the two seconds waiting time in the `time.Sleep()` function, the second print statement could be immediately completed, and the `main()` function could terminate before the goroutine is completed.
71+
72+
## Codebyte Example
73+
74+
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:
75+
76+
```codebyte/golang
77+
package main
78+
import (
79+
"fmt"
80+
"time"
81+
)
82+
83+
// define a function
84+
func print(text string) {
85+
fmt.Println(text)
86+
}
87+
88+
func main() {
89+
// call goroutine
90+
go print("This text is from the goroutine.")
91+
92+
// call function
93+
print("This text is from the main function.")
94+
95+
time.Sleep(time.Second * 1)
96+
}
97+
```

0 commit comments

Comments
 (0)