Sobes.tech
Middle+

How to organize communication between two goroutines?

sobes.tech AI

Answer from AI

In Go, channels are commonly used for communication between two goroutines. Channels allow safe data transfer and synchronization between goroutines.

Example of simple message exchange through a channel:

package main

import (
    "fmt"
)

func main() {
    ch := make(chan string)

    go func() {
        ch <- "Hello from goroutine"
    }()

    msg := <-ch
    fmt.Println(msg)
}

In this example, one goroutine sends a string to the channel, and the main goroutine receives it and prints it. Channels provide safe and synchronized data transfer between goroutines.

How to organize communication between two goroutines… - sobes.tech