【问题标题】:Why can't I change the values in a range of type structure?为什么我不能更改一系列类型结构中的值?
【发布时间】:2018-12-08 21:31:52
【问题描述】:

这是我的第一篇文章,所以请放轻松。 :) ...我非常熟悉许多传统的编程语言,但我是 Go 新手,无法理解切片和范围的使用。下面的程序代码和 cmets 说明了我的惊愕。谢谢!

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)
}

输出看起来像这样... Screenshot Output

提前感谢您的建议。

【问题讨论】:

标签: go range slice member


【解决方案1】:

The Go Programming Language Specification

For statements with range clause

带有“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)
}

【讨论】:

  • 或者直接使用chartRecords[i].Count = mm
  • @AndySchweig:更新后的elem.Count 稍后在fmt.Printf 中使用。
【解决方案2】:

在我朋友 Russ 的帮助下添加这些行。

Quote: 嗨,维克多,如果我没记错的话,当你在一个集合中...范围时,返回的对象是集合中保存的原始对象的副本(“值语义”)。 因此, elem 变量是您分配计数的副本。这是有效的代码,但您并没有像预期的那样更新集合。 考虑将你的 for...range 循环调整为:

    fmt.Printf("======NOW MODIFY VALUES THIS WAY========\r\n")                                  
    i = 0
    for idx := range chartRecords { 
        mm := modMe(mod, i)  
        chartRecords[idx].Count = mm  
        fmt.Printf("No: %2d | Count: %2d | Name = %s\r\n", i, chartRecords[idx].Count, chartRecords[idx].Name)  //Print out this elem.Count element in the range
        i = i + 1    
    } 

    fmt.Printf("======CHECK AGAIN AND VALUES ARE AS DESIRED========\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
    } 

现在我理解得更好了,我希望这对像我这样的其他新手有所帮助......

祝你有美好的一天!

维克多

【讨论】:

    猜你喜欢
    • 2020-10-24
    • 2015-09-18
    • 1970-01-01
    • 2018-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多