当前位置:网站首页>Golang的range
Golang的range
2022-07-03 08:04:00 【Bel_Ami同学】
Golang range实例
range 是 golang中特别常用的一种遍历方式,走C++入门的看到这样的遍历方式感觉太好用了。但是如果没有认真思考过range的工作原理,在一些特定的场景使用range,可能并不能达到预期的效果。
1.1 range基础语法
首先我们先来看一下range的基础语法
一开始认识golang 感觉这门语言对语法的检测特别严谨。比如定义但是没有使用的变量就会报错。
package main
import "fmt"
func main() {
x := []string{
"a", "b", "c"}
for v := range x {
fmt.Println(v) //prints 0, 1, 2
}
for _, v := range x {
fmt.Println(v) //prints a, b, c
}
}
很明显第一个range返回值为索引值index,第二个为值value。一般常见的都是第二种方式 _ ,v = range x,用_来丢弃不用的值。当只用一个值来接收range返回的时候也不会报错,这个就很意外了,并且这个时候返回的为索引值。
1.2 range原理
然后我们来看一下range的意外状况
package main
import "fmt"
type student struct {
Name string
Age int
}
func main() {
m := make(map[string]*student)
stus := []student{
{
Name: "zhou", Age: 24},
{
Name: "li", Age: 23},
{
Name: "wang", Age: 22},
}
for _, stu := range stus {
m[stu.Name] = &stu
}
for k, v := range m {
fmt.Println(k, "=>", v.Name)
}
}
//输出情况如下,因为遍历map,index为键,value为map中对应的值,每次遍历是无序的
/* li => wang wang => wang zhou => wang */
但是初次遇到这种情况,大家就会发现这和预期的输出不一样。整个map中的所有实值都是wang。为什么会是这样?
然后我们尝试以下边的代码做输出
fmt.Println(m)
//此时输出内容如下
//map[zhou:0xc000046400 li:0xc000046400 wang:0xc000046400]
我们会发现map[string]*student 中的 *student最终存储的值都是一样的。究其原因如下:range 是使用一个副本重复赋值的方式来遍历每一个目标元素的,可以将其视为一个目标元素类型的变量,每一次遍历迭代就会把目标元素拷贝到range准备的副本,并作返回。
修改版:
for k, stu := range stus {
fmt.Println(stu.Name, "=>", &stu)
m[stus[k].Name] = &stus[k]
}
for k,v:=range m{
println(k,"=>",v.Name)
}
或者
for i:=0;i<len(stus);i++ {
m[stus[i].Name] = &stus[i]
}
for k,v:=range m{
println(k,"=>",v.Name)
}
下面是两个range嵌套使用实例:
// setReqBodyLog 请求的body数据落日志
func setReqBodyLog(ctx context.Context, req *http.Request) {
formMap := map[string]string{
}
for k, v := range req.PostForm {
for _, formV := range v {
if formMap[k] == "" {
formMap[k] = desensitization.RemoveSensetive(formV)
} else {
formMap[k] += "," + desensitization.RemoveSensetive(formV)
}
}
}
logit.ReplaceMetaFields(ctx, logit.AutoField(transmitEntities.LogReqBody, formMap))
}
边栏推荐
- Worldview satellite remote sensing image data / meter resolution remote sensing image
- idea取消引用顯示效果
- The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
- 一个实习生的CnosDB之旅
- JS regular case-
- Microsoft Security Response Center
- E: Unable to locate package ROS melody desktop full
- Huawei switch basic configuration (telnet/ssh login)
- Yolo series --- xml2txt script
- Haproxy+kept cluster setup 02
猜你喜欢

【cocos creator】点击按钮切换界面

VMware virtual machine configuration static IP

An intern's journey to cnosdb

Haproxy+kept build 01

Unity change default editor

创业团队如何落地敏捷测试,提升质量效能?丨声网开发者创业讲堂 Vol.03

A tunnel to all ports of the server
![[cocos creator] Click the button to switch the interface](/img/b8/f0fd54a2a197cbfd788990e2806b52.png)
[cocos creator] Click the button to switch the interface

the installer has encountered an unexpected error installing this package

haproxy+keepalived集群搭建02
随机推荐
Storage of data
Technical dry goods | some thoughts on the future of AI architecture
Xlua task list youyou
Microsoft Security Response Center
static关键字
Technical dry goods | thinking about the unification of dynamic and static diagrams of AI framework
Register keyword
Use filechannel to copy files
P2622 light off problem II (state compression search)
[untitled]
Maxcompute string splitting function -split_ PART
Wechat applet taro learning record
Product creation and commercial realization of chat robot (according to La Ma Bang - Dr. Wang Jingjing - speech)
Wechat native applet cloud development learning record 01
PIP uses image website to solve the problem of slow network speed
idea取消引用顯示效果
什么是定义?什么是声明?它们有何区别?
Shader foundation 01
2020-12-12
Unity2019_ Natural ambient light_ Sky box