我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

初始化从0到N的整数的golang slice

I am almost certain that I read about a simple "tricky" way to initialize slice of ints with the numbers from 0 to N, but I cannot find it anymore.

What is the simplest way to do this?

You just use make passing N for the length then use a simple for loop to set the values...

mySlice := make([]int, N)
for i := 0; i < N; i++ {
      mySlice[i] = i
}

Here's a full example on play; https://play.golang.org/p/yvyzuWxN1M

近期文章

  • 输入字节2564处的错误base64数据错误消息
  • 为什么地址大小不同? (0x206a10-0x104382e0)
  • 可以用一种语言或库创建的jwt用另一种语言或库解码和验证吗?
  • 如何编写一个可以接受多种类型之一的函数?
  • 缓冲区问题不能在http.NewRequest golang的参数中将<type>用作类型io.Reader
  • 是否可以在设计用于go generate的模板上使用gofmt?
  • 在go中读取US-ASCII文件
  • 如何更改结构变量内容?
  • Golang-无法解析JSON
  • 主线程永不屈服于goroutine
  • 使用Go在Windows资源管理器中显示文件?
  • 对我的Golang Web服务器进行基准测试
  • Golang元帅动态XML元素名称
  • Golang,在Linux中调用新的系统调用
  • 为什么struct缓冲区不需要初始化
  • tls.Conn在golang中是“ goroutine安全的”吗?
  • Emacs:无法打开加载文件,自动完成
  • 分布式Go的框架(例如Erlang OTP)? [关闭]
  • 我们在Golang中有与Java和C#对象相同的东西吗?
  • 如何在Golang中初始化嵌套结构数组? [重复]

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华