Golang chan close + select

package main

import (
	"fmt"
	"time"
)

func main() {
	ch := make(chan int, 1)
	go func() {
		ch <- 1
		close(ch)
	}()
	for {
		select {
		case i, ok := <-ch:
			if ok {
				time.Sleep(1*time.Second)
				fmt.Println("-----------")
				fmt.Println(i)
			} else {
				time.Sleep(1*time.Second)
				fmt.Println("+++++++++++")
				fmt.Println(i)
			}
		default:
			time.Sleep(100000000)
			break
		}
	}
}
Copy the code