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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改