dongye1912 2017-01-28 06:49
浏览 54
已采纳

切片数组-GO

1)

For below slice declaration,

var a [][3]string

creates a single slice(a) that points to array of 3 strings, len(a) = 3 and cap(a) =3 but not cap(a) >= 3

a = make([][3]string, 5)

creates 5 slices, where each slice(say a[0]) points to array of 3 strings, len(a[0]) = 3 and cap(a[0]) = 3


2)

For slice declaration,

var b [][3][6]string

creates a single slice(b) that points to 3 arrays of 6 strings each, where len(b) = 3 and cap(b) =3

b = make([][3][6]string, 5)

creates 5 slices, where each slice(say b[0]) points to 3 arrays of 6 strings each


In both cases, after making slices, I said, creates 5 slices,


Considering the syntax, below are my two questions,

a = make([][3]string, 5)

My question:

1)

Is it 5 slices, where each slice(a[0]) is array of 3 strings?

or

Is it a single slice(a) pointing to 5 arrays of 3 strings each?

2)

How do I know the type of an element? slice or array?

1) In such scenarios, "array of" terminology has to be cautiously used.

2) There are no implicit pointers involved in GO unlike C

  • 写回答

2条回答 默认 最新

  • dongxing4805 2017-01-28 06:59
    关注

    1) Considering the syntax,

    a = make([][3]string, 5)
    

    Is it 5 slices, where each slice(a[0]) is array of 3 strings?

    or

    Is it a single slice(a) pointing to 5 arrays of 3 strings each?

    Its a single slice with 5 elements where each element is an array of 3 strings.

    2)

    How do I know the type of an element? slice or array?

    Slices and arrays are different types. You can not interchangeably assign arrays and slices to the same variable, hence if the declaration declares it as an array, its an array, if it declares it as a slice, its a slice. If it has a number in the brackets ([5]string) its an array, if the brackets are empty ([]string) its a slice.

    2) For slice declaration,

    var b [][3][6]string
    

    creates a single slice(b) that points to 3 arrays of 6 strings each, where len(b) = 3 and cap(b) =3

    b = make([][3][6]string, 5)
    

    creates 5 slices, where each slice(say b[0]) points to 3 arrays of 6 strings each

    No. Former code just declares a variable, it doesn't hold a slice yet. Latter code creates one slice with five elements.

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

报告相同问题?

悬赏问题

  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错