Home > Blockchain >  writing to buffer channel more than its capacity
writing to buffer channel more than its capacity

Time:10-03

overwriting the buffer channel more than its capacity does have any effects ?

Since there is another go routine and main go routine doesn't join with it so no deadlock here

    package main
    
    import "fmt"
    
    func main() {
        ch := make(chan int, 2)
        go func (){
        ch <- 1
        ch <- 2
        ch <- 4//blocks here but scheduler picked up another go routine
        ch <- 6
        ch <- 10
        //close(ch)
        }()
        fmt.Println(<-ch)
        fmt.Println(<-ch)
        //for v:=range ch{
        //fmt.Println(<-ch)//1 2 4 6 10
        //}

        
    }

CodePudding user response:

because of that no deadlock here

A go build -race . would indeed detect no race condition.

But the main reason there is no deadlock, is that the main function exits after the second fmt.Println(<-ch).

Even if the anonymous goroutine is blocked on ch <- 4, the all program stops anyway.

  • Related