Basic Go Routines (like Threading)
·
194 words
·
1 minute read
This script basically just prints out the numbers 1 to 10, but it does so by spinning off each print statement into it’s own thread, using a go routine. You’ll notice the script also has a start and an end print statement, but because go routines are used, the end statement will be printed before and items (1-10) are printed.
The ending sleep allows the go routines to execute as they were being cut short by the main function finished, thus ending the programs life.
|
|
Output:
Start Script
End Script
Print Item: 1
Print Item: 2
Print Item: 3
Print Item: 4
Print Item: 5
Print Item: 6
Print Item: 7
Print Item: 8
Print Item: 9
Print Item: 10