duanjing1023 2015-07-17 14:22
浏览 14
已采纳

开始-分配多个变量时出错

I am learner of go language. This is very basic question. Please check the below sample code, and look into 3rd line.

a := [3]int{10,20} 
var i int = 50
i, a[2] = 100, i

fmt.Println(i)   //100
fmt.Println(a)   //[10 20 50]

I have overwriten the value 100 in i variable and immediately applied the int array. When I printed the array, the new value was not printed. How does multiple variable assignment work in go? Why the i value is not updated into the array immediately?

  • 写回答

2条回答 默认 最新

  • duanning9110 2015-07-17 14:43
    关注

    The assigment section of the Go spec mentions:

    The assignment proceeds in two phases.

    That means:

    var i int = 50
    i, a[2] = 100, i
    
    • a[2] is assigned the i evaluated before assignment (50)
    • i is assigned 100
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图