Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

Goroutines

This Go code demonstrates the basics of goroutines and how they interact with the main program. It illustrates the basics of goroutines, showing how they can run concurrently with the main program and the use of time.Sleep for synchronization.

Let's break it down with inline comments:

package main

import (
	"fmt"
	"time"
)

// Function to print messages three times
func f(from string) {
	for i := 0; i < 3; i++ {
		fmt.Println(from, ":", i)
	}
}

func main() {
	// Calling the function in the main goroutine
	f("direct")

	// Launching a new goroutine to execute the function concurrently
	go f("goroutine")

	// Using an anonymous function in a goroutine with a parameter
	go func(msg string) {
		fmt.Println(msg)
	}("going")

	// Giving some time for the goroutines to finish before exiting the program
	time.Sleep(time.Second)

	// Print "done" after all goroutines have completed
	fmt.Println("done")
}

Output

direct : 0
direct : 1
direct : 2
going
goroutine : 0
goroutine : 1
goroutine : 2
done

Explanation:

  1. package main: Indicates that this Go file belongs to the main executable package.

  2. import (...): Imports necessary packages, including "fmt" for formatting and printing, and "time" for handling time-related operations.

  3. func f(from string) { ... }: Defines a function f that takes a string parameter from and prints a message three times with the given prefix.

  4. func main() { ... }: The main function, where the execution of the program begins.

  5. f("direct"): Calls the function f in the main goroutine, printing a message directly.

  6. go f("goroutine"): Launches a new goroutine to execute the function f("goroutine") concurrently, allowing it to run independently of the main program.

  7. go func(msg string) { ... }("going"): Creates an anonymous function and launches it as a goroutine with a parameter. This demonstrates how to use goroutines with inline function definitions.

  8. time.Sleep(time.Second): Introduces a delay of one second, giving the goroutines some time to complete before the program exits. This is a simple way to synchronize the main goroutine with the others.

  9. fmt.Println("done"): Prints "done" after the goroutines have had enough time to execute, indicating the end of the program.

PreviousErrorNextChannels

Last updated 1 year ago

Was this helpful?