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 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式