Chanel

随便写了个tour练习

 

Go的chanel体验还是不错的,共享地址

package main

import "fmt"

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

func main() {
    s := []int{1, 2, 3, 4, 5}
    c := make(chan int)
    go sum(s[:len(s) / 2], c)
    go sum(s[len(s) / 2:], c)
    x, y := <- c, <-c

    fmt.Println(x, y, x + y)


}

 

 

--

package main

type Tree struct {
    Left *Tree
    Value int
    Right *Tree
}

func Walk(t *Tree, ch chan int) {
    if t.Left != nil {
        Walk(t.Left, ch)
    }
    ch <- t.Value
    if t.Right != nil {
        Walk(t.Right, ch)
    }
    return
}

func Same(t1, t2 *Tree) bool  {
    ch1, ch2 := make(chan int), make(chan int)
    go Walk(t1, ch1)
    go Walk(t2, ch2)
    if <-ch1 == <-ch2 {
        return true
    } else {
        return false
    }
}

 

end,最近会专注于Go的学习

posted @ 2020-02-12 23:37  zhangyu63  阅读(138)  评论(0编辑  收藏  举报