Complete the code to close the channel.
ch := make(chan int)
// Close the channel
[1](ch)The close function is used to close a channel in Go.
Complete the code to receive from a closed channel without blocking.
ch := make(chan int)
go func() {
close(ch)
}()
value, [1] := <-chThe second value when receiving from a channel indicates if the channel is open (true) or closed (false).
Fix the error in the code that tries to send on a closed channel.
ch := make(chan int)
go func() {
close(ch)
}()
// This will cause a panic
[1] ch <- 5Sending on a closed channel causes a panic. To fix it, do not send after closing.
Fill both blanks to create a map comprehension that stores lengths of words longer than 3 characters.
words := []string{"go", "code", "chat", "bot"}
lengths := map[string]int{}
for _, word := range words {
if len(word) [1] 3 {
lengths[[2]] = len(word)
}
}The condition checks if the word length is greater than 3, and the map key is the word itself.
Fill all three blanks to create a select statement that handles sending, receiving, and default case on a channel.
select {
case [1] <- 10:
fmt.Println("Sent 10")
case val := <-[2]:
fmt.Println("Received", val)
default:
fmt.Println([3])
}The select sends 10 on channel ch, receives from ch, and prints "No communication" if neither is ready.