一尘不染

为什么不能更改类型结构范围内的值?

go

这是我的第一篇文章,所以请对我“轻松”。:)
…我对许多传统的编程语言非常熟悉,但是我是Go语言的新手,很难理解切片和范围的用法。下面的程序代码和注释说明了我的震惊。谢谢!

package main

import (
    "fmt"
    "time"
)

type myStruct struct {
    Name  string
    Count int
}

编写了自己的Mod函数,因为在Go库中找不到。

func modMe(mod int, value int) int {
    var m int
    var ret int

    m = value / mod
    ret = value - m*mod
    return ret
}

func main() {
    mod := 4
    cnt := 16
    fmt.Printf("Record mod is %d\r\n", mod)

声明结构数组并添加一些记录

    chartRecords := []myStruct{}

    for i := 0; i<=cnt ;i++ {
        n := myStruct{Count: i, Name: fmt.Sprintf("Joe%2d", i)}                         //Load some data
        chartRecords = append(chartRecords,n)
    }

加载数据会产生我期望的输出

    fmt.Printf("======ORIGINAL LOADED VALUES========\r\n")                                  
    i := 0
    for _, elem := range chartRecords {
        fmt.Printf("No: %2d | Count: %2d | Name = %s\r\n", i, elem.Count, elem.Name)    //Print out original values in the range
        i = i + 1
    }

现在,我修改值并打印它们,以查看它们是否显示为已修改。这看起来像预期的那样。

    fmt.Printf("======MODIFIED VALUES EXPECTED========\r\n")                                    
    i = 0
    for _, elem := range chartRecords {                                                 //looping thru the range of the data records
        mm := modMe(mod, i)                                                             //modMe is my function to return the Mod of a number based on moduls 'mod'
        elem.Count = mm                                                                 //assigning the new mod value to Count
        fmt.Printf("No: %2d | Count: %2d | Name = %s\r\n", i, elem.Count, elem.Name)    //Print out this elem.Count element in the range
        i = i + 1                                                                       
    }

现在,我只需再次遍历同一范围并打印出相同内容即可。但是输出显示原始值。我不明白为什么会这样。我猜想它与切片和添加值而不是替换值有关。

    fmt.Printf("======CHECK AGAIN AND VALUES ARE BACK TO ORIGINAL========\r\n")                                         //Now lets loop through the same range
    i = 0
    for _, elem := range chartRecords {
        fmt.Printf("No: %2d | Count: %2d | Name = %s\r\n", i, elem.Count, elem.Name)    //Print out this elem.Count element in the range
        i = i + 1
    }                                                                                   //But this output shows the original values  WHY??
    time.Sleep(60 * time.Second)
}

输出看起来像这样… 屏幕截图输出

预先感谢您的建议。


阅读 240

收藏
2020-07-02

共1个答案

一尘不染

Go编程语言规范

对于带有范围子句的语句

带有“ range”子句的“
for”语句遍历数组,切片,字符串或映射的所有条目,或通道上接收到的值。对于每个条目,它会将迭代值分配给相应的迭代变量(如果存在),然后执行该块。


Go编程语言规范很容易阅读。

将更新后的elem迭代变量放回chartRecords切片中:

for i, elem := range chartRecords {
    elem.Count = modMe(mod, i)
    chartRecords[i] = elem
    fmt.Printf("No: %2d | Count: %2d | Name = %s\r\n", i, elem.Count, elem.Name)
}
2020-07-02