Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

go - Why can't I change the values in a range of type structure?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...