duanhuchang5056 2016-05-17 05:04
浏览 41
已采纳

如何在Go中分配非恒定大小的2D数组? [重复]

This question already has an answer here:

I want to allocate space for a 2 dimensional array and I want to do so in a clean way.

address [10]int
myLength := len(address)

// 1. but this not work at all
matrix := [myLength][myLength]byte

// 2. and this initializes 10 arrays with length 0
matrix := make([][]int, myLength, myLength)

I am getting an error for the first attempt:

non-constant array bound l

PS not solved by: How to allocate a non-constant sized array in Go

</div>
  • 写回答

1条回答 默认 最新

  • dongyuanliao6204 2016-05-17 05:12
    关注

    There's no "one liner" way to do that. simply do:

    matrix := make([][]int, myLength)
    for i : = 0; i < myLength; i++ {
       matrix[i] = make([]int, myLength)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分