清风细雨楼 Design By www.eepep.com

看程序:

package main
import (
  "fmt"
  "time"
)
func main() {
  str := []string{"I","like","Golang"}
  for _, v := range str{
   v += "good"
  }
  for k, v := range str{
   fmt.Println(k, v)
  }
  time.Sleep(1e9)
}

结果:

0 I
1 like
2 Golang

想一想,为什么。

再看:

package main
import (
  "fmt"
  "time"
)
func main() {
  str := []string{"I","like","Golang"}
  for k, v := range str{
   fmt.Println(&k, &v)
  }
  time.Sleep(1e9)
}

结果:

0xc000012050 0xc00000e1e0
0xc000012050 0xc00000e1e0
0xc000012050 0xc00000e1e0

想一想,为什么。

再看:

package main
import (
  "fmt"
  "time"
)
func main() {
  str := []string{"I","like","Golang"}
  for k, v := range str{
   str = append(str, "good")
   fmt.Println(k, v)
  }
  time.Sleep(1e9)
}

结果:

0 I
1 like
2 Golang

想一想, 为什么。

再看:

package main
import (
  "fmt"
  "time"
)
func main() {
  str := []string{"I","like","Golang"}
  for k, v := range str{
   go func(i int, s string){
    fmt.Println(i, s, k, v)
   }(k, v)
  }
  time.Sleep(1e9)
}

结果:

0 I 2 Golang
1 like 2 Golang
2 Golang 2 Golang

想一想,为什么。

最后再看:

package main
import (
  "fmt"
  "time"
)
func main() {
  str := []string{"I","like","Golang"}
  for k, v := range str{
   go func(i int, s string){
    fmt.Println(i, s, k, v)
   }(k, v)
   time.Sleep(1e9)
  }
  time.Sleep(5e9)
}

结果:

0 I 0 I
1 like 1 like
2 Golang 2 Golang

想一想,为什么。

不多说。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接

清风细雨楼 Design By www.eepep.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
清风细雨楼 Design By www.eepep.com