dsf5989 2018-06-12 05:44
浏览 35

在golang中创建地图数组的地图

I want to create a json in golang for which I need to first create map of the following:

{"inputs": [{"data": {"image": {"url": "SOME_URL"}}}]}

how to create this map on golang. (for now even hardcoded will also work for me)

  • 写回答

2条回答 默认 最新

  • dongpan2788 2018-06-12 06:00
    关注

    In a struct:

    type SomeData struct {
        Inputs []struct {
            Data struct {
                Image struct {
                    URL string `json:"url"`
                } `json:"image"`
            } `json:"data"`
        } `json:"inputs"`
    }
    

    But if we wanted to be able to add things individually, AND be more idiomatic, we would do it like this:

    type Image struct {
        URL string `json:"url"`
    }
    
    type Data struct {
        Image Image `json:"image"`
    }
    
    
    type Input struct {
        Data Data `json:"data"`
    }
    
    type SomeData struct {
        Inputs []Input `json:"inputs"`
    }
    

    Then, of course, we could always just use a map: someData := map[interface{}]interface{}{}

    It really just depends on which route you'd like to go. I suggest the second one as it gives you better fine-grained tooling without any pesky dirty tricks or code-clutter.

    Hope this helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?