Memory leaks Golang Goroutine泄漏 //版权所有2012 Go作者。版权所有。 //此源代码的使用受BSD样式的约束 //可以在许可证文件中找到的许可证。 //+构建忽略 包干管 进口( “fmt” “code.google.com/p/go-tour/tree” ) func walkImpl(t*tree.tree,ch chan int){ 如果t==nil{ 返回 } walkImpl(t.左,ch) ch

Memory leaks Golang Goroutine泄漏 //版权所有2012 Go作者。版权所有。 //此源代码的使用受BSD样式的约束 //可以在许可证文件中找到的许可证。 //+构建忽略 包干管 进口( “fmt” “code.google.com/p/go-tour/tree” ) func walkImpl(t*tree.tree,ch chan int){ 如果t==nil{ 返回 } walkImpl(t.左,ch) ch,memory-leaks,go,goroutine,Memory Leaks,Go,Goroutine,当检测到差异时,程序停止在通道上接收 walk goroutines一直运行,直到它们阻止发送到通道。它们永远不会退出。这就是泄漏。第二种解决方案通过使用quit通道处理泄漏。当quit通道关闭(在相同的()函数中)时,select语句的情况2成功(谢谢,我也知道第二个是如何退出这些程序的。非常感谢。 // Copyright 2012 The Go Authors. All rights reserved. // Use of this source code is governed by

当检测到差异时,程序停止在通道上接收


walk goroutines一直运行,直到它们阻止发送到通道。它们永远不会退出。这就是泄漏。

第二种解决方案通过使用quit通道处理泄漏。当quit通道关闭(在相同的()函数中)时,select语句的情况2成功(谢谢,我也知道第二个是如何退出这些程序的。非常感谢。
// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
    "fmt"

    "code.google.com/p/go-tour/tree"
)

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

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    walkImpl(t, ch)
    // Need to close the channel here
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
// NOTE: The implementation leaks goroutines when trees are different.
// See binarytrees_quit.go for a better solution.
func Same(t1, t2 *tree.Tree) bool {
    w1, w2 := make(chan int), make(chan int)

    go Walk(t1, w1)
    go Walk(t2, w2)

    for {
        v1, ok1 := <-w1
        v2, ok2 := <-w2
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Print("tree.New(1) == tree.New(1): ")
    if Same(tree.New(1), tree.New(1)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }

    fmt.Print("tree.New(1) != tree.New(2): ")
    if !Same(tree.New(1), tree.New(2)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }
}
// Copyright 2015 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
    "fmt"

    "code.google.com/p/go-tour/tree"
)

func walkImpl(t *tree.Tree, ch, quit chan int) {
    if t == nil {
        return
    }
    walkImpl(t.Left, ch, quit)
    select {
    case ch <- t.Value:
        // Value successfully sent.
    case <-quit:
        return
    }
    walkImpl(t.Right, ch, quit)
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch, quit chan int) {
    walkImpl(t, ch, quit)
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    w1, w2 := make(chan int), make(chan int)
    quit := make(chan int)
    defer close(quit)

    go Walk(t1, w1, quit)
    go Walk(t2, w2, quit)

    for {
        v1, ok1 := <-w1
        v2, ok2 := <-w2
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Print("tree.New(1) == tree.New(1): ")
    if Same(tree.New(1), tree.New(1)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }

    fmt.Print("tree.New(1) != tree.New(2): ")
    if !Same(tree.New(1), tree.New(2)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }
}