为什么我不能更改一系列类型结构中的值?
来源:Golang技术栈
时间:2023-04-11 15:31:04 230浏览 收藏
小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《为什么我不能更改一系列类型结构中的值?》,以下内容将会涉及到golang,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!
问题内容
这是我的第一篇文章,所以请对我“走”轻松。:) ...我非常熟悉许多传统的编程语言,但我是 Go 新手,无法理解切片和范围的使用。下面的程序代码和注释说明了我的惊愕。谢谢!
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加载数据会产生我期望的输出
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) }输出看起来像这样... 屏幕截图输出
提前感谢您的建议。
正确答案
带有“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) }理论要掌握,实操不能落!以上关于《为什么我不能更改一系列类型结构中的值?》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!
声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
439 收藏
-
262 收藏
-
193 收藏
-
188 收藏
-
500 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
476 收藏
-
486 收藏
-
439 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 541次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 506次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习