dongniaoli1822 2018-12-19 15:02
浏览 49
已采纳

切片的(整数)索引作为结构数组

Is it possible in Go create array with where each element of array will be array of slices or structures.

Something like in PHP

    $a = [1=>"test", 2=>""]
    // in this example 2 is integer will be for GoLang?
    $a[2] = [ object, object, object ]

Can I do in Go something like ? I know about incorrect syntax.

   var a [int][]StructureName

   b := make([]StructureName, 0)
   b := append ( b, StructureName{a, b, c, d})
   b := append ( b, StructureName{e, f, g, h})

   a[0] = append (a[0][0], b)

`/*
[
1 => [
    ‘username1’, <-- string element
    ‘events’=>[ <-- array of structures
        object1, <-- structure
        object2, <-- structure
        object3 <-- structure
        ]
    ],
2 => [ <-- next record
    ‘username2’,
    ‘events’=>[
        object1,
        object2,
        object3
        ]
    ]
]
*/
`
  • 写回答

2条回答 默认 最新

  • duanlu1793 2018-12-19 15:18
    关注

    You can use map and map it from int => interface{}.

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        m := make(map[int]interface{})
        var x []interface{}
        x = append(x, "username")
        x = append(x, []int{3, 1, 3})
        m[1] = x
        fmt.Println(m)
    }
    

    https://play.golang.org/p/B_dsvARic8c

    Hope this helps.

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

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来