当前位置: 首页 > wzjs >正文

中牟高端网站建设网站建设与运营 就业

中牟高端网站建设,网站建设与运营 就业,建设网站询价对比表模板,化妆品推广策划方案Go 切片导致 rand.Shuffle 产生重复数据的原因与解决方案 在 Go 语言的实际开发中,切片(slice)是一种非常灵活的数据结构。然而,由于其底层数据共享的特性,在某些情况下可能会导致意想不到的 Bug。 本文将详细分析 r…

Go 切片导致 rand.Shuffle 产生重复数据的原因与解决方案

在 Go 语言的实际开发中,切片(slice)是一种非常灵活的数据结构。然而,由于其底层数据共享的特性,在某些情况下可能会导致意想不到的 Bug。

本文将详细分析 rand.Shuffle 之后,切片中的数据出现重复的问题,探讨其根本原因,并给出最佳解决方案,以确保代码的正确性和稳定性。


🔍 问题描述

在一个 Go 服务端 API 里,我们需要按照 curBatch 参数进行分页,从 interestCfg 里分批选取 interestTagNum 个兴趣标签,并在返回结果前对选中的数据进行随机打乱。

全部兴趣标签示例:

{"InterestTags": [{"interestName":"Daily Sharing"},{"interestName":"Gaming"},{"interestName":"AI"},{"interestName":"test"},{"interestName":"Sports"},{"interestName":"Cars"},{"interestName":"other"}]
}

🌟 现象回顾

curBatch = 0 时,返回的数据是正确的:

{"InterestTags": [{ "interestName": "Daily Sharing" },{ "interestName": "Gaming" },{ "interestName": "AI" }]
}

但当 curBatch = 2 时,测试环境出现了数据重复的问题:(本地运行正常)

1. 不随机时(正确的结果):
{"InterestTags": [{ "interestName": "other" },{ "interestName": "Daily Sharing" },{ "interestName": "Gaming" }]
}
2. 随机后(错误的结果):
{"InterestTags": [{ "interestName": "Gaming" },{ "interestName": "Gaming" },{ "interestName": "AI" }]
}

问题:

  • “Gaming” 出现了两次,而 “test” 消失了!
  • 本地环境正常,但测试环境异常,导致调试变得困难。

🔎 问题排查

数据的选择和随机操作逻辑如下:

interestTags := make([]model.InterestConfig, 0, interestConfig.InterestTagNum)// 处理interestConfig,根据curBatch分批次处理
if len(interestConfig.InterestCfg) > 0 && interestConfig.InterestTagNum > 0 {interestAllTags := interestConfig.InterestCfgnumBatches := (len(interestAllTags) + int(interestConfig.InterestTagNum) - 1) / int(interestConfig.InterestTagNum)startIdx := (curBatch % numBatches) * int(interestConfig.InterestTagNum)endIdx := startIdx + int(interestConfig.InterestTagNum)if endIdx > len(interestAllTags) {interestTags = interestAllTags[startIdx:]interestTags = append(interestTags, interestAllTags[:(endIdx-len(interestAllTags))]...)} else {interestTags = interestAllTags[startIdx:endIdx]}
}// 随机打乱 interestTags 顺序
r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Shuffle(len(interestTags), func(i, j int) {interestTags[i], interestTags[j] = interestTags[j], interestTags[i]
})

关键点分析

  1. interestTags = interestAllTags[startIdx:endIdx] 直接从 interestAllTags 取出数据,但切片是引用类型,因此 interestTags 共享了 interestAllTags 的底层数组
  2. rand.Shuffle 随机交换 interestTags 里的元素,但 interestTags 指向 interestAllTags,可能导致原始数据被错误修改
  3. 本地和测试环境不一致,可能与 Go 运行时的内存管理机制高并发场景下的切片扩容行为有关。

🛠 代码验证

为了验证 interestTags 是否共享 interestAllTags 的底层数组,我们打印切片元素的内存地址:

fmt.Println("Before Shuffle:")
for i, tag := range interestTags {fmt.Printf("[%d] %p: %s\n", i, &interestTags[i], tag.InterestName)
}r.Shuffle(len(interestTags), func(i, j int) {interestTags[i], interestTags[j] = interestTags[j], interestTags[i]
})fmt.Println("After Shuffle:")
for i, tag := range interestTags {fmt.Printf("[%d] %p: %s\n", i, &interestTags[i], tag.InterestName)
}

测试环境的 After Shuffle 结果中,某些索引的地址相同,证明 rand.Shuffle 影响了原始数据,导致元素重复。


💡 解决方案

方案 1:使用 append 进行数据拷贝

为了避免 interestTags 共享 interestAllTags 的底层数组,我们需要显式拷贝数据:

interestTags = make([]model.InterestConfig, 0, interestConfig.InterestTagNum)
if endIdx > len(interestAllTags) {interestTags = append(interestTags, interestAllTags[startIdx:]...)interestTags = append(interestTags, interestAllTags[:(endIdx-len(interestAllTags))]...)
} else {interestTags = append(interestTags, interestAllTags[startIdx:endIdx]...)
}

🔹 为什么这样做?

  • append(..., interestAllTags[startIdx:endIdx]...) 创建新的切片,避免 interestTags 共享 interestAllTags 的底层数据。
  • 独立的数据拷贝 确保 rand.Shuffle 只影响 interestTags,不会破坏原始 interestAllTags

📌 总结

🌟 1. 问题原因

  • Go 切片是引用类型,直接赋值 interestTags = interestAllTags[startIdx:endIdx] 不会创建新数据,而是共享底层数组
  • rand.Shuffle 可能影响 interestAllTags,导致元素重复
  • 本地环境正常,但测试环境异常,可能与 Go 内存管理切片扩容策略有关。

🌟 2. 解决方案

  • 使用 append 进行数据拷贝,确保 interestTags 是独立的数据,避免 rand.Shuffle 影响原始 interestAllTags

🚀 经验总结

  1. Go 切片是引用类型,不能直接赋值,否则可能共享底层数据。
  2. 使用 rand.Shuffle 之前,必须确保数据是独立的副本
  3. 尽量使用 append 创建新的切片,避免底层数组共享问题。
  4. 不同环境表现不一致时,应检查内存管理、并发情况及数据结构副作用。

文章转载自:

http://G7FfPAVv.nspbj.cn
http://W3191ltc.nspbj.cn
http://JHlCisks.nspbj.cn
http://ZeOH9NXf.nspbj.cn
http://4V7AW80n.nspbj.cn
http://IDybbGC6.nspbj.cn
http://547IhjTb.nspbj.cn
http://p4ZNTYwx.nspbj.cn
http://rKLh309i.nspbj.cn
http://DbQVnNhg.nspbj.cn
http://OhIBFgdy.nspbj.cn
http://RwMspAme.nspbj.cn
http://EFluDsYh.nspbj.cn
http://orOg0jCN.nspbj.cn
http://kw1fUuEi.nspbj.cn
http://tMEUPs0f.nspbj.cn
http://o9N6sa13.nspbj.cn
http://3Wr4pcL1.nspbj.cn
http://OgjklxTT.nspbj.cn
http://utf8QgiL.nspbj.cn
http://bt6SODeF.nspbj.cn
http://pOLLKQJs.nspbj.cn
http://MvH0sX9r.nspbj.cn
http://DsqtuxwL.nspbj.cn
http://CJR8ebjS.nspbj.cn
http://6lMEPmpZ.nspbj.cn
http://Gyzxxcqm.nspbj.cn
http://dd4pBO0a.nspbj.cn
http://D5nfb7Ed.nspbj.cn
http://F8AjNv92.nspbj.cn
http://www.dtcms.com/wzjs/609777.html

相关文章:

  • 网站页面大小优化怎么做WordPress1001无标题
  • 网站建设 制作什么是所见即所得的网页制作工具
  • 大型网站建设托管服务广西建设网桂建云网站
  • 免费开设网站移动互联网项目创业融资计划书
  • 偷网站源码直接建站室内设计师测评网
  • 网站建设要求报告WordPress手机端底部悬浮窗
  • 广州市建设交易中心网站首页简单又快的科学小制作
  • 门户网站建设 简报wordpress验证支付宝
  • 微知微网站建设实训平台商城网站前台html模板
  • 做网站域名需要在哪里备案网站风格细节
  • 阿里云怎么做淘宝客网站东莞网站SEO优化托管
  • 兰山网站建设公司网络运营怎么做
  • 做app和做网站相同和区别房地产销售现状
  • 外国黄冈网站推广软件wordpress分类添加轮播图
  • 机械网站建设公司推荐沈阳网络科技公司排名
  • 盐城亭湖区建设局网站重庆做网站好的公司
  • 网站网站到底怎么做帝国cms小说阅读网站模板
  • 从事网站开发需要什么上海专业网站制作设计
  • 网站开发的有哪些好的软件seo分析师
  • 长春网站建设那家好网络营销常用工具有哪些?
  • 提高网站互动性学做网网站论坛
  • 公司网站建设的通知装饰设计有限公司经营范围
  • 广州比较好的网站建设企业用redis加速wordpress
  • 环保网站案例制作手机软件
  • 知名网站开发wordpress linux权限
  • 运城市做网站网站被墙是谁做的
  • 深圳网站设计廊坊公司跨国贸易平台有哪些
  • 厦门做网站优化价格网站业务
  • 织梦大气婚纱影楼网站源码 dedecms摄影工作室网站模板微信公众号怎么建网站
  • 绍兴网站建设设计制作wordpress 域名 去掉