Sobes.tech
Senior

Mida saab kasutada andmete edastamiseks goroutine'ide vahel?

sobes.tech AI

Vastus AI-lt

To transmit data between goroutines in Go, channels are commonly used. Channels provide safe synchronization and data transfer, allowing goroutines to exchange values without explicit locking.

Example of using a channel:

ch := make(chan int)

// Sending data in a goroutine
go func() {
    ch <- 42
}()

// Receiving data
value := <-ch
fmt.Println(value) // 42

Besides channels, shared memory with synchronization via mutex from the sync package can be used, but channels are considered a more idiomatic way in Go.