Category: Golang

  • 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: Non-Blocking Channel Operations

    In Go, channels can also be used in a non-blocking way. This means that you can send or receive from a channel without blocking the program’s execution. The select statement can be used to manage non-blocking channel operations. In this example, the program uses non-blocking receives and sends with the select statement to handle channel…

  • Go by Example: Timeouts

    A timeout is an upper bound on the amount of time that can be spent waiting for an operation to complete. Here’s an example to demonstrate timeouts in Go: In the example above, the first select case will timeout after 1 second, and the second select case will receive a value from c2 after 2…

  • Go by Example: Select

    The “select” statement is used to choose one out of multiple communication operations. The statement blocks until one of its communication operations can proceed, which then proceeds with the associated value. If multiple communication operations are ready, one is selected randomly. In this example, two goroutines are used to send values to two separate channels.…

  • Go by Example: Channel Directions

    This program demonstrates the use of channel directions. The ping function takes a channel pings of type chan<- string, meaning it can only send strings to it. The pong function takes two channels, pings of type <-chan string, meaning it can only receive strings from it, and pongs of type chan<- string, meaning it can…

  • Go by Example: Channel Synchronization

    Here’s an example to demonstrate channel synchronization: In this example, we create a channel done and a function worker that takes the channel as an argument. The worker function does some work, waits for a second and then sends a value true to the channel done. In the main function, we start the worker function…

  • Go by Example: Channel Buffering

    Here’s an example that demonstrates channel buffering: In this example, we create a channel c with a buffer size of 2. This means that c can store up to 2 values before blocking. We then send the values 1 and 2 to the channel, which are stored in the buffer. Finally, we receive the two…

  • Go by Example: Channels

    Channels in Go allow you to pass values between concurrent goroutines. Channels can be thought of as pipes that connect concurrent goroutines and allow them to communicate. Here’s an example: In the example above, we create a channel of strings using make(chan string). Then, we create an anonymous function that sends the string “ping” to…

  • Go by Example: Goroutines

    Here’s a simple example of using goroutines in Go: In this example, we create a new channel c using make(chan int). Then, we start a new goroutine using the go keyword and call the printNumbers function with c as an argument. In the printNumbers function, we use a for loop to print numbers from 0…

  • Go by Example: Errors

    Here’s an example in Go that demonstrates how to handle errors: In this example, we define a function divide that takes two int values as input and returns the result of the division. The function also returns an error value to indicate whether or not an error occurred during the division. We use the errors.New…