The main function will have to create the channel, pass it to sum as a parameter, and then read a single value from it.
package main
import (
"fmt"
)
func main() {
c := make(chan int)
x := []int{1, 2, 3, 4, 5, 6, 7, 8, 9}
// pass the data and the return channel to the goroutine
go sum(x, c)
// read a single value from the channel
fmt.Println(<-c)
}
func sum(x []int, c chan<- int) {
total := 0
for _, v := range x {
total += v
}
// write the result to the channel we got from main
c <- total
}