select
package main

import "sync"

func main() {
	var wg sync.WaitGroup
	foo := make(chan int)
	bar := make(chan int)
	wg.Add(1)
	go func() {
		defer wg.Done()
		select {
		case foo <- <-bar:
		default:
			println("default")
		}
	}()
	wg.Wait()
}
Answer
Try it
fatal error: all goroutines are asleep - deadlock!
select1
package main

import "fmt"

func main() {
	ch1 := make(chan int)
	ch2 := make(chan int)
	go func() {
		ch1 <- 1
	}()

	go func() {
		select {
		case <-ch1:
		case ch2 <- 2:
		}
	}()

	fmt.Println(<-ch2)
}
  • A. 1
  • B. 2
  • C. No output, program is deadlocked
  • D. No output, program has finished execution.
  • E. else
Answer
Try it
B