dstk51319 2012-07-08 18:27
浏览 48
已采纳

Go中的Python样式生成器

I'm currently working through the Tour of Go, and I thought that goroutines have been used similarly to Python generators, particularly with Question 66. I thought 66 looked complex, so I rewrote it to this:

package main

import "fmt"

func fibonacci(c chan int) {
    x, y := 1, 1

    for {
        c <- x
        x, y = y, x + y
    }
}

func main() {
    c := make(chan int)
    go fibonacci(c)

    for i := 0; i < 10; i++ {
        fmt.Println(<-c)
    }
}

This seems to work. A couple of questions:

  1. If I turn up the buffer size on the channel to say, 10, fibonacci would fill up 10 further spots, as quickly as possible, and main would eat up the spots as quickly as it could go. Is this right? This would be more performant than a buffer size of 1 at the expense of memory, correct?
  2. As the channel doesn't get closed by the fibonacci sender, what happens memory-wise when we go out of scope here? My expectation is that once c and go fibonacci is out of scope, the channel and everything on it gets garbage-collected. My gut tells me this is probably not what happens.
  • 写回答

4条回答 默认 最新

  • duanhe4267 2012-07-10 22:22
    关注

    I like @tux21b's answer; having the channel created in the fib() function makes the calling code nice and clean. To elaborate a bit, you only need a separate 'quit' channel if there's no way to tell the function when to stop when you call it. If you only ever care about "numbers up to X", you can do this:

    package main
    
    import "fmt"
    
    func fib(n int) chan int {
        c := make(chan int)
    
        go func() {
            x, y := 0, 1
    
            for x < n {
                c <- x
                x, y = y, x+y
            }
    
            close(c)
        }()
    
        return c
    }
    
    func main() {
        // Print the Fibonacci numbers less than 500
        for i := range fib(500) {
            fmt.Println(i)
        }
    }
    

    If you want the ability to do either, this is a little sloppy, but I personally like it better than testing the condition in the caller and then signalling a quit through a separate channel:

    func fib(wanted func (int, int) bool) chan int {
        c := make(chan int)
    
        go func() {
            x, y := 0, 1
    
            for i := 0; wanted(i, x); i++{
                c <- x
                x, y = y, x+y
            }
    
            close(c)
        }()
    
        return c
    }
    
    func main() {
        // Print the first 10 Fibonacci numbers
        for n := range fib(func(i, x int) bool { return i < 10 }) {
            fmt.Println(n)
        }
    
        // Print the Fibonacci numbers less than 500
        for n := range fib(func(i, x int) bool { return x < 500 }) {
            fmt.Println(n)
        }
    }
    

    I think it just depends on the particulars of a given situation whether you:

    1. Tell the generator when to stop when you create it by
      1. Passing an explicit number of values to generate
      2. Passing a goal value
      3. Passing a function that determines whether to keep going
    2. Give the generator a 'quit' channel, test the values yourself, and tell it to quit when appropriate.

    To wrap up and actually answer your questions:

    1. Increasing the channel size would help performance due to fewer context switches. In this trivial example, neither performance nor memory consumption are going to be an issue, but in other situations, buffering the channel is often a very good idea. The memory used by make (chan int, 100) hardly seems significant in most cases, but it could easily make a big performance difference.

    2. You have an infinite loop in your fibonacci function, so the goroutine running it will run (block on c <- x, in this case) forever. The fact that (once c goes out of scope in the caller) you won't ever again read from the channel you share with it doesn't change that. And as @tux21b pointed out, the channel will never be garbage collected since it's still in use. This has nothing to do with closing the channel (the purpose of which is to let the receiving end of the channel know that no more values will be coming) and everything to do with not returning from your function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突