当前位置:网站首页>An interview question about common pitfalls in golang for range
An interview question about common pitfalls in golang for range
2022-07-23 20:51:00 【youngqqcn】
The following code runs , What will be output ?
package main
import "fmt"
type student struct {
Name string
Age int
}
func pase_student() {
m := make(map[string]*student)
stus := []student{
{
Name: "A", Age: 1},
{
Name: "B", Age: 2},
{
Name: "C", Age: 3},
}
for _, stu := range stus {
m[stu.Name] = &stu
}
for k, v := range m {
fmt.Printf("%v,%v,%v\n", k, v.Name, v.Age)
}
}
func main() {
pase_student()
}
Please think about it. , The answer is at the end of the article
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
,
answer
A,C,3
B,C,3
C,C,3
Why? ?
Because in golang Of for range in , Temporary variable stu Will be reused , Then copy the new value , Without opening up another space . So in the title m[stu.Name] = &stu, It can lead to stu The value in will be the last copied value , namely stus Last value .
We can write a test program , Verify this “ pit ”:
func main() {
a := []int{
1, 2, 3, 4}
for x, n := range a {
fmt.Printf("%d: %d\n", x, n)
fmt.Printf("%p\n", &n)
}
}
Output
0: 1
0xc0000160c8
1: 2
0xc0000160c8
2: 3
0xc0000160c8
3: 4
0xc0000160c8
You can see , Every time &n It's all fixed
How to modify ?
The first 1 Kind of , Using value , Instead of Pointers . Can be m := make(map[string]*student) Change to m := make(map[string]student) , The complete code is as follows :
func pase_student() {
m := make(map[string]student)
stus := []student{
{
Name: "A", Age: 1},
{
Name: "B", Age: 2},
{
Name: "C", Age: 3},
}
for _, stu := range stus {
m[stu.Name] = stu
}
for k, v := range m {
fmt.Printf("%v,%v,%v\n", k, v.Name, v.Age)
}
}
The first 2 Kind of , no need for range Switch to classic for
func pase_student() {
m := make(map[string]student)
stus := []student{
{
Name: "A", Age: 1},
{
Name: "B", Age: 2},
{
Name: "C", Age: 3},
}
for i := 0; i < len(stus); i++ {
m[stus[i].Name] = stus[i]
}
for k, v := range m {
fmt.Printf("%v,%v,%v\n", k, v.Name, v.Age)
}
}
Other methods can also .
therefore , In actual project development , be used for range Be sure to pay attention to this when assigning values “ pit ”
边栏推荐
- Kubevela offline installation
- 深度学习-NLP经典论文、课程、论文等资源整理分享
- 现在完全不知道怎么同步
- 实践数据湖iceberg 第三十七课 kakfa写入iceberg的 icberg表的 enfource ,not enfource测试
- 第十一天:续第十天BGP的基本配置
- Solve the problem that the user clicks quickly and repeats the request within 1 second
- OpenLayers实例-Animated GIF-GIF动画
- STM32C8t6 驱动激光雷达实战(二)
- Shell command and operation principle
- "Pulse" to the future! Huawei cloud Mrs helps smooth migration to the cloud
猜你喜欢

高数下|三重积分的计算1|高数叔|手写笔记

高数下|二重积分的计算2|高数叔|手写笔记

速卖通选品推荐:韩国市场有哪些潜力机会商品?

OpenLayers实例-Advanced Mapbox Vector Tiles-高级Mapbox矢量贴图

If the order is not paid within 30 minutes, it will be automatically cancelled

使用高德地图JS API 2.0加载起点终点路径轨迹

A beautiful road
![[PDD interview] analyze the activity of applications (cameras) in mobile phones](/img/c7/cc7d43b02f4aa09ded2c7161236123.png)
[PDD interview] analyze the activity of applications (cameras) in mobile phones

哈希表、无序集合、映射的原理与实现

Chapter 3 business function development (creating clues)
随机推荐
当我们在谈论陈春花和华为时,我们到底在讨论什么?
STM32c8t6驱动激光雷达(一)
OpenIM重大升级-群聊读扩散模型发布 群管理功能升级
CDR插件开发之Addon插件003 - 认识解决方案(sln)和项目(csproj)文件
[leetcode] day101 rotating image
NLP hotspots from ACL 2022 onsite experience
使用高德地图JS API 2.0加载起点终点路径轨迹
第十一天:续第十天BGP的基本配置
Kubevela offline installation
一文读懂研发效能洞察的五大流动指标
(Note)优化器Adam的学习率设置
一道golang中关于for range常见坑的面试题
Educational codeforces round 132 A-D problem solution
A beautiful road
手机股票开户安全吗?
【云驻共创】天天写SQL,你遇到了哪些神奇的特性?
TROPOMI(哨兵5P)数据介绍及下载方法
第十二天:续第十一天(BGP相关知识)
“脉”向未来!华为云MRS助力脉脉迁移平滑上云
Himawari-8 数据介绍及下载方法