dongqu7778 2018-02-16 23:07
浏览 25
已采纳

golang多重作业评估

I'm confused on the concept of multiple assignment. Given the following code:

func fibonacci() func() int {
    current, next := 0, 1
    return func() int {
        current, next = next, current+next
        return current
    }
}

How is the assignment evaluated, given the fact that both variables appear on both the left and right side of the assignment?

  • 写回答

3条回答 默认 最新

  • dougan0529 2018-02-17 01:24
    关注

    The Go Programming Language Specification

    Assignments

    The assignment proceeds in two phases. First, the operands of index expressions and pointer indirections (including implicit pointer indirections in selectors) on the left and the expressions on the right are all evaluated in the usual order. Second, the assignments are carried out in left-to-right order.


    The usual example to illustrate multiple assignments is a swap. For example,

    package main
    
    import "fmt"
    
    func main() {
        {
            i, j := 7, 42
            fmt.Println(i, j)
            // swap i and j - implicit temporaries
            i, j = j, i
            fmt.Println(i, j)
        }
    
        fmt.Println()
    
        {
            i, j := 7, 42
            fmt.Println(i, j)
            // swap i and j - explicit temporaries
            ti, tj := i, j
            i, j = tj, ti
            fmt.Println(i, j)
        }
    }
    

    Playground: https://play.golang.org/p/HcD9zq_7tqQ

    Output:

    7 42
    42 7
    
    7 42
    42 7
    

    The one statement multiple assignment, which uses implicit temporary variables, is equivalent to (a shorthand for) the two multiple assignment statements, which use explicit temporary variables.


    Your fibonacci example translates, with explicit order and temporary variables, to:

    package main
    
    import "fmt"
    
    func fibonacciMultiple() func() int {
        current, next := 0, 1
        return func() int {
            current, next = next, current+next
            return current
        }
    }
    
    func fibonacciSingle() func() int {
        current, next := 0, 1
        return func() int {
    
            // current, next = next, current+next
            // first phase, evaluation, left-to-right
            t1 := next
            t2 := current + next
            // second phase, assignmemt, left-to-right
            current = t1
            next = t2
    
            return current
        }
    }
    
    func main() {
        m := fibonacciMultiple()
        fmt.Println(m(), m(), m(), m(), m(), m())
        s := fibonacciSingle()
        fmt.Println(s(), s(), s(), s(), s(), s())
    }
    

    Playground: https://play.golang.org/p/XFq-0wyNke9

    Output:

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

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置