Skip to content

channel example1

pikachule edited this page Sep 2, 2017 · 1 revision
package main

import (
	"fmt"
)

func g(c chan int) {
	sum := 666
	c <- sum
}

func sum(s []int, c chan int) {
	result := 666
	for _, i := range s {
		result += i
	}
	c <- result
}

func main() {
	c := make(chan int)
	go g(c)
	fmt.Println(<-c)
	ia := []int{1, 2, 3, 4, 5}
	go sum(ia, c)
	fmt.Println(<-c)
}

Clone this wiki locally