2019-08-29 09:56
浏览 96

添加表达式“ fmt.Println()”时发生了什么

I`m a tiro about the golang and during the exploring the channel,I was comfused by following code ,so anyone who can tell me the differences between that?

when I run the code,the console logs -5,17 And if I make the comment use ,I get different result 17,-5 I don`t know what happend ...

golang version is the lastest one

//comman func
func sum(a []int, c chan int) {
    total := 0
    for _, v := range a {
        total += v
    c <- total  // send total to c
func main (){
    a := []int{7, 2, 8, -9, 4, 0}

    c := make(chan int)
    go sum(a[:len(a)/2], c)
    go sum(a[len(a)/2:], c)
    gh,w33 :=  <-c, <-c

I expect the two times of results are 17,-5 ,but when the comment isn`t useful ,the result is -5 17

1条回答 默认 最新

相关推荐 更多相似问题