首页 > 其他分享 >Go 100 mistakes - #32: Ignoring the impact of using pointer elements in range loops

Go 100 mistakes - #32: Ignoring the impact of using pointer elements in range loops

时间:2024-02-20 20:34:59浏览次数:17  
标签:impact Customer elements map 32 customer using pointer

This section looks at a specific mistake when using a range loop with pointer elements. If we’re not cautious enough, it can lead us to an issue where we reference the wrong elements. Let’s examine this problem and how to fix it.
Before we begin, let’s clarify the rationale for using a slice or map of pointer elements. There are three main cases:
 In terms of semantics, storing data using pointer semantics implies sharing the element. For example, the following method holds the logic to insert an element into a cache:

Here, using the pointer semantics implies that the Foo element is shared by both the caller of Put and the Store struct.
 Sometimes we already manipulate pointers. Hence, it can be handy to store pointers directly in our collection instead of values.

 If we store large structs, and these structs are frequently mutated, we can use pointers instead to avoid a copy and an insertion for each mutation:

Because updateMapPointer accepts a map of pointers, the mutation of the foo field can be done in a single step.

 

Now it’s time to discuss the common mistake with pointer elements in range loops. We will consider the following two structs:
 A Customer struct representing a customer
 A Store that holds a map of Customer pointers

The following method iterates over a slice of Customer elements and stores them in the m map:

In this example, we iterate over the input slice using the range operator and store Customer pointers in the map. But does this method do what we expect?
Let’s give it a try by calling it with a slice of three different Customer structs:

Here’s the result of this code if we print the map:

As we can see, instead of storing three different Customer structs, all the elements stored in the map reference the same Customer struct: 3. What have we done wrong?
Iterating over the customers slice using the range loop, regardless of the number of elements, creates a single customer variable with a fixed address. We can verify this by printing the pointer address during each iteration:

Why is this important? Let’s examine each iteration:
 During the first iteration, customer references the first element: Customer 1. We store a pointer to a customer struct.
 During the second iteration, customer now references another element: Customer 2. We also store a pointer to a customer struct.
 Finally, during the last iteration, customer references the last element: Customer 3. Again, the same pointer is stored in the map.
At the end of the iterations, we have stored the same pointer in the map three times (see figure 4.4). This pointer’s last assignment is a reference to the slice’s last element:Customer 3. This is why all the map elements reference the same Customer.

 

标签:impact,Customer,elements,map,32,customer,using,pointer
From: https://www.cnblogs.com/zhangzhihui/p/18023983

相关文章

  • 微控制器STM32L475RCT7[IC MCU 32BIT 256KB]、AZ5A25-01F.R7G瞬态抑制二极管(TVS),AONS
    1、微控制器STM32L475RCT7[ICMCU32BIT256KBFLASH64LQFP]STM32L475RC器件是基于高性能ARM®Cortex®-M432位RISC内核的超低功耗微控制器,工作频率高达80MHz。Cortex-M4内核具有浮点单元(FPU)单精度,支持所有ARM单精度数据处理指令和数据类型。它还实现了完整的DSP指令集和存储......
  • 【三分钟开服仅32/月】幻兽帕鲁服务器最新一键部署以及修改游戏参数保姆教程
    前言《幻兽帕鲁》是Pocketpair开发的一款开放世界生存制作游戏,游戏于2023年11月2日至11月5日进行了封闭网络测试,于2024年1月18日发行抢先体验版本。游戏中,玩家可以在广阔的世界中收集神奇的生物“帕鲁”,派他们进行战斗、建造、做农活,工业生产等游戏支持单人游玩&创建本地房间(最......
  • STM32 ---SPI通讯
    I2C能够通过软件模拟,同样的,SPI通讯也可以通过软件模拟,具体需要掌握SPI的收发时序。但在本节,我们着重讲解STM32的硬件SPI外设。 我们知道SPI有以下几个特点1、时钟频率:Fpclk/2,4,8,16,32,64,128,256(Fpclk是时钟分频,APH2的Fpclk是72MHZ,APB1的Fpclk是36MHZ)2、支持多主机模型......
  • 【linux新手起步02】vi编辑时出现E325:ATTENTION。
    vi编辑时出现E325:ATTENTION一、原因二、解决方法:rm+swap文件路径以及名称一.原因:出现这个问题,是因为由于在编辑该文件的时候异常退出,因为vim在编辑文件时会创建一个交换文件swapfile以保证文件的安全性。点击查看代码E325:ATTENTIONFoundaswapfilebythen......
  • STM32笔记
    整理有道云笔记中,关于STM32的笔记Keil软件中map文件解析简单的说:map文件是通过编译器编译之后,集程序、数据及IO空间的一种映射文件。遇到内存越界,或者溢出的情况,可以通过map文件,去确定函数大小,入口地址等一些重要信息。我们在Keil中最常见的就是在编译之后,编译窗口会显示类......
  • Modscan32 软件最全使用详解
    软件使用手动连接点击菜单栏”连接设置(Connection)“->”连接(Connect)“,弹出连接配置窗口。在”使用的连接”那里选择:RemotemodbusTCPServer RemoteTELNETServerDirectConnectiontoCOM1DirectConnectiontoCOM2…DirectConnectiontoCOM32备注:”Direct......
  • 在 Visual Studio 2022 中创建一个类似于旧版本 Visual Studio 中的 Win32 Console Ap
    以下内容来自AI的回答,实测有效在VisualStudio2022中创建一个项目,其自动生成的源文件内容包含#include"stdafx.h"和使用_tmain作为入口点,意味着你需要创建一个基于Windows的传统控制台应用程序,这通常与旧版本的VisualStudio(如VisualStudio2005或更早)和使用预......
  • 基于stm32的spi接口dma 数据收发实例解析
    一前记  SPI接口平时用的比较少,再加上对CUBEMX不是很熟悉,这里踩了不少坑才把问题解决。针对遇到了不少问题,是要值得梳理一下了。二源码解析1SPI的DMA发送端配置: 2主函数源码:uint32_tg_spi_cnt=0;voidHAL_SPI_TxCpltCallback(SPI_HandleTypeDef*hspi){......
  • STM32 SPI接口 DMA normal 和circual区别
     DMA有normal和circular两种模式。circular模式:就调用这个函数一次就可以了,DMA一直开启,一帧数据发送完毕之后里面发送下一帧,中间没有停顿。这样确实是快了,也释放了CPU,各路的数据采集因为缺少了等待串口发送的时间,所以就间接提高的了数据更新速率。但有个致命缺陷:数据采集和数......
  • 01. 初识STM32
    一、什么是STM32  STM32,从字面上来理解,ST是意法半导体,M是Microelectronics的缩写,32表示32位,合起来理解,STM32就是指ST公司开发的32位微控制器。  STM32主要分两大块,MCU和MPU,MCU就是我们常见的STM32微控制器,不能跑Linux,而MPU则是ST在19年才推出的微......