duanliaoyu8419 2018-06-29 16:24
浏览 50
已采纳

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

This is my first post so please "Go" easy on me. :) ... I am quite familiar with many traditional programming languages but I am new to Go and having trouble understanding the use of slices and ranges. The program code and comments below illustrate my consternation. Thank you!

package main

import (
    "fmt"
    "time"
)

type myStruct struct {
    Name  string
    Count int
}

Wrote my own Mod function because I could not find on in the Go libraries.

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
", mod)

Declare a structure array and add some records

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

Loading the data produces the output that I expect

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

Now I modify the values and print them to see that they appear modified. This looks as expected.

    fmt.Printf("======MODIFIED VALUES EXPECTED========
")                                    
    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
", i, elem.Count, elem.Name)    //Print out this elem.Count element in the range
        i = i + 1                                                                       
    }

Now I simply loop through the same range again and print the same thing out. But the output shows the original values. I don't understand why this is happening. I'm guessing that it has something to do with slices and adding values rather than replacing the values.

    fmt.Printf("======CHECK AGAIN AND VALUES ARE BACK TO ORIGINAL========
")                                         //Now lets loop through the same range
    i = 0
    for _, elem := range chartRecords {
        fmt.Printf("No: %2d | Count: %2d | Name = %s
", 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)
}

The output looks like this... Screenshot Output

Thanks in advance for your advice.

  • 写回答

2条回答 默认 最新

  • dongzhiyan5693 2018-06-29 16:29
    关注

    The Go Programming Language Specification

    For statements with range clause

    A "for" statement with a "range" clause iterates through all entries of an array, slice, string or map, or values received on a channel. For each entry it assigns iteration values to corresponding iteration variables if present and then executes the block.


    The Go Programming Language Specification is an easy read.

    Put the updated elem iteration variable back in the chartRecords slice:

    for i, elem := range chartRecords {
        elem.Count = modMe(mod, i)
        chartRecords[i] = elem
        fmt.Printf("No: %2d | Count: %2d | Name = %s
    ", i, elem.Count, elem.Name)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。