Go Channel's doubts

an official demo

package main

import "fmt"

func sum(s []int, c chan int) {
    sum := 0
    for _, v := range s {
        sum += v
    }

    c <- sum // send sum to c
}

func main() {
    s := []int{7, 2, 8, -9, 4, 0}

    c := make(chan int)

    go sum(s[:len(s)/2], c) // 7,2,8 = 17

    go sum(s[len(s)/2:], c) // -9,4,0 = -5

    x, y := <-c, <-c // receive from c
    //
    fmt.Println(x, y, x+y) // 17,-5,12
}

normally, according to the FIFO result, it should be 17 br 5jue 12 < why the printed result is-5je 17je 12.
I don"t understand. Ask the boss to answer

Oct.03,2021

go sum () is executed asynchronously. You have two tasks. The order in which the two tasks are executed first and then is uncertain.

Menu