当前位置:网站首页>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))
}
边栏推荐
- C语言-入门-精华版-带你走进编程(一)
- My touch screen production "brief history" 2
- idea取消引用显示效果
- The difference between hdmi2.1 and hdmi2.0 and the conversion of PD signals.
- Pat class a 1031 Hello world for u
- Huawei switches are configured with SSH login remote management switches
- Docker installs MySQL and successfully uses Navicat connection
- Generate video using clipout in viz engine
- idea取消引用顯示效果
- About Wireshark's unsuccessful installation of npcap
猜你喜欢

数据库应用技术课程设计之商城管理系统

oracle 插入单引号

一个实习生的CnosDB之旅

What is a data type? What is the use of data types?
![[step on the pit series] MySQL failed to modify the root password](/img/d0/f975baf18bac506208abff3713ac03.png)
[step on the pit series] MySQL failed to modify the root password

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

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

Project experience sharing: handwritten Chinese character recognition based on Shengsi mindspire

Haproxy+kept cluster setup 02

the installer has encountered an unexpected error installing this package
随机推荐
Transplantation of freetype Library
Viz artist advanced script video tutorial -- stringmap use and vertex operation
PostGIS space function
Ilruntime learning - start from scratch
Xlua task list youyou
*p++、*++p、++*p、(*p)++
Wechat applet taro learning record
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Pycharm remote ssh pyenv error: pydev debugger: warning: trying to add breakpoint to file that does
Docker installs MySQL and successfully uses Navicat connection
oracle 插入单引号
Uniapp learning records
What is a data type? What is the use of data types?
Unity performance optimization
[MySQL 14] use dbeaver tool to remotely backup and restore MySQL database (Linux Environment)
vcs import src < ros2. Repos failed
How to establish rectangular coordinate system in space
[MySQL 11] how to solve the case sensitive problem of MySQL 8.0.18
When did you find out that youth was over
什么是定义?什么是声明?它们有何区别?