douzhan8652 2019-03-13 16:00
浏览 29
已采纳

动态二维矩阵

I have this code, which is giving me errors:

package main

import (
    "fmt"
)

func main() {
        var cnt = make([][]string,0,10)
        for i := 0; i < 5; i++ {
             var tmp = make([]string,0,8)
             for c := 0 ; c < 5 ; c++ {
                 tmp = append(tmp,"Matias")
              }
              cnt= append(cnt,tmp...)
         }
    fmt.Println(cnt)
}

It's giving me an error. Basically what I need is to have the slice to be as dinámic as possible. I don't know what the final length will be in any of the two dimensions.

  • 写回答

1条回答 默认 最新

  • duanlian1320 2019-03-13 16:08
    关注

    The compiler error is actually misleading - it should quote you are using tmp... which is of a variadic of strings - instead it quotes tmp which is of the correct type []string which one could use to append to cnt:

    main.go:14:15:cannot use tmp (type []string) as type [][]string in append

    Anyway, using tmp..., go is turning tmp from a []string into individual string parameters. Effectively:

    cnt = append(cnt, tmp[0], tmp[1], tmp[2], tmp[3], tmp[4])
    

    And go can't append string to a [][]string type.

    Change the line to:

    cnt = append(cnt, tmp)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line