go channel 并发也会有并发问题

package main

import (
	"fmt"
	"time"
)


func main() {

	ch := make(chan bool, 10)
	if ch == nil {
		fmt.Println("ch is nil")
	}


	go func() {
		for i := 0; i < 100; i ++ {
			if i % 2 == 0 {
				ch = nil
			}else {
				ch = make(chan bool, 10)
				time.Sleep(1 * time.Second)
			}
		}


		//fmt.Printf("go 1 new &ch: %v \n", ch)
	}()

	go func() {
		//fmt.Printf("go 2 &ch: %v \n", ch)
		for {
			select {
			case ch <- true:
				fmt.Println("enter true case")
			default:
				fmt.Println("enter default case ")
			}
		}

	}()


	time.Sleep(10 * time.Second)
}

一个线程读chan,一个线程不断修改chan,也会有data race报错。

var code = "c9f6ec1e-d994-47a2-9691-8fc7207d30f8"
posted @ 2023-02-07 09:54  影随风动91  阅读(11)  评论(0编辑  收藏  举报