Category: Golang

  • Go by Example: Mutexes

    A mutex is a synchronization mechanism that controls access to a shared resource in a concurrent environment. In Go, the sync package provides a Mutex type that can be used to implement mutual exclusion, ensuring that only one Goroutine can access a shared resource at any given time. Here’s an example of using a Mutex…

  • Go by Example: Atomic Counters

    Atomic operations are a type of operation that are executed as a single, uninterruptible sequence of instructions. In Go, the sync/atomic package provides a set of atomic operations that can be used to safely access and modify shared data without the risk of concurrent access. Here’s an example of using an atomic counter in Go:…

  • Go by Example: WaitGroups

    A WaitGroup is a type in Go’s standard library that allows multiple goroutines to wait for a set of operations to complete. It’s used to wait for a collection of Goroutines to finish. Here’s an example of using a WaitGroup: In this example, 5 worker goroutines are started and the WaitGroup wg is used to…

  • Go by Example: Worker Pools

    A worker pool is a pattern in concurrent programming where a fixed number of worker goroutines are created to perform tasks from a shared task queue. This helps control the number of concurrent tasks and reduces the overhead of creating and destroying worker goroutines for every task. Here is an example implementation of a worker…

  • Go by Example: Tickers

    A ticker is a special type of timer that repeatedly sends a value on a channel at regular intervals. Here’s an example: In this example, we create a ticker that sends a value on ticker.C channel every 500 milliseconds. The channel is read by a goroutine which repeatedly selects the value sent on it. After…

  • Go by Example: Timers

    A Timer represents a single event in the future. You tell the Timer how long you want to wait, and it provides a channel that will be notified at that time. In this example, a timer is created with a duration of 2 seconds and waits for it to expire using the <-timer1.C channel. Another…

  • Go by Example: Range over Channels

    The range a keyword can be used to iterate over the values received from a channel. The iteration stops when the channel is closed or has no more values to receive. Here’s an example to demonstrate the use of range with channels: This program creates a channel queue of size 5, sends values 1, 2,…

  • Go by Example: Closing Channels

    Closing a channel indicates that no more values will be sent on the channel. This can be useful to communicate completion to the channel’s receivers. A sender can close a channel by using the close built-in function. Closing a channel is not the same as sending the zero value on the channel. When a channel…

  • Go by Example: Range over Channels

    The range keyword can be used to iterate over the values received from a channel. The iteration stops when the channel is closed or has no more values to receive. Here’s an example to demonstrate the use of range with channels: This program creates a channel queue of size 5, sends values 1, 2, and…

  • Go by Example: Timers

    A Timer represents a single event in the future. You tell the Timer how long you want to wait, and it provides a channel that will be notified at that time. In this example, a timer is created with a duration of 2 seconds and waits for it to expire using the <-timer1.C channel. Another…