doutany76678 2014-11-24 23:59 采纳率: 0%
浏览 62
已采纳

创建数组的组合文字

I want to create a composite literal of arrays of arrays within a struct. Outside of a struct

package main

import "fmt"

func main() {
  x := [...][]string {{"a", "b"}}

  fmt.Printf("%s", x)
}

works. (http://play.golang.org/p/C2RbNnd7LL)

But I can't define a field of type [...][]string within a struct. As in http://play.golang.org/p/wHNeeuAJuO

package main

import "fmt"

type t struct {
    f    [...][]string
}

func main() {
  x := [...][]string {{"a", "b"}}
  y := t{x}
  fmt.Printf("%s", y)
}

f gives the errror use of [...] array outside of array literal

  • 写回答

2条回答 默认 最新

  • dongtie0929 2014-11-25 01:15
    关注

    The problem is that [...]elementType is not a valid type name. You can use the syntax with the initialiser syntax like [...]int{1, 2, 3, 4} because the compiler can see how many elements there are to determine the actual type ([4]int in this case).

    If you want an array-like type that doesn't have a fixed compile time size, use a slice:

    type t struct {
        f    [][]string
    }
    

    Alternatively, if the number of elements is fixed at compile time and you still want to use an array, you will need to specify the actual element count in your type definition.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 关于游戏c++语言代码问题
  • ¥15 如何制作永久二维码,最好是微信也可以扫开的。(相关搜索:管理系统)
  • ¥15 delphi indy cookie 有效期
  • ¥15 labelme打不开怎么办
  • ¥35 按照图片上的两个任务要求,用keil5写出运行代码,并在proteus上仿真成功,🙏
  • ¥15 免费的电脑视频剪辑类软件如何盈利
  • ¥30 MPI读入tif文件并将文件路径分配给各进程时遇到问题
  • ¥15 pycharm中导入模块出错
  • ¥20 Ros2 moveit2 Windows环境配置,有偿,价格可商议。
  • ¥15 有关“完美的代价”问题的代码漏洞