Sobes.tech
Junior — Senior

Example of using channels and goroutines in Go

livecode

Task condition

What will be the output of the following Go program?

package main

import (
 "fmt"
 "time"
)

func main() {
 dataCh := make(chan int)
 go func() {
  dataCh <- 1
 }()
 time.Sleep(time.Millisecond * 500)
 close(dataCh)

 for val := range dataCh {
  fmt.Println(val)
 }

 time.Sleep(time.Millisecond * 100)
}