为什么我不能更改一系列类型结构中的值?
Why can't I change the values in a range of type structure?
这是我的第一个 post 所以请 "Go" 放轻松。 :) ... 我对许多传统编程语言都非常熟悉,但我是 Go 的新手,无法理解切片和范围的使用。下面的程序代码和评论说明了我的惊愕。谢谢!
package main
import (
"fmt"
"time"
)
type myStruct struct {
Name string
Count int
}
写了我自己的 Mod 函数,因为我在 Go 库中找不到 on。
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
提前感谢您的建议。
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.
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)
}
在我的朋友 Russ 的帮助下添加这些行。
Quote:嗨,Victor,如果我没记错的话,当你在一个集合中进行...范围搜索时,返回的对象是集合中保存的原始对象的副本(“值语义”)。
因此,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
}
现在我明白了,我希望这能帮助像我这样的新手...
祝你有愉快的一天!
维克多
这是我的第一个 post 所以请 "Go" 放轻松。 :) ... 我对许多传统编程语言都非常熟悉,但我是 Go 的新手,无法理解切片和范围的使用。下面的程序代码和评论说明了我的惊愕。谢谢!
package main
import (
"fmt"
"time"
)
type myStruct struct {
Name string
Count int
}
写了我自己的 Mod 函数,因为我在 Go 库中找不到 on。
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
提前感谢您的建议。
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.
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)
}
在我的朋友 Russ 的帮助下添加这些行。
Quote:嗨,Victor,如果我没记错的话,当你在一个集合中进行...范围搜索时,返回的对象是集合中保存的原始对象的副本(“值语义”)。 因此,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
}
现在我明白了,我希望这能帮助像我这样的新手...
祝你有愉快的一天!
维克多