Complete the code to send a value into the channel.
ch := make(chan int)
func main() {
go func() {
ch[1] 42
}()
val := <-ch
println(val)
}To send a value into a channel in Go, use the syntax ch <- value.
Complete the code to receive a value from the channel.
ch := make(chan string)
func main() {
go func() {
ch <- "hello"
}()
msg := [1]ch
println(msg)
}To receive a value from a channel, use the syntax value := <-ch.
Fix the error in the code to properly synchronize using a channel.
done := make(chan bool)
func main() {
go func() {
println("Working...")
[1] <- true
}()
<-done
}To send a value into the channel, use done <- true. The channel variable name must be on the left.
Fill both blanks to create a map of word lengths for words longer than 3 characters.
words := []string{"go", "code", "chat", "ai"}
lengths := map[string]int{
[1]: [2] for _, word := range words if len(word) > 3
}word.len().The key should be the word itself, and the value should be its length using len(word).
Fill all three blanks to create a synchronized counter using a channel.
counter := 0 increment := make(chan bool) go func() { for { <-[1] [2]++ println([3]) } }() func main() { increment <- true }
The channel to receive from is increment. The counter variable is incremented and printed.
