drj58429 2016-09-12 15:27
浏览 12

如何在golang中定义匿名数据结构

I am new to golang and you may find my question very silly. But still I have tried my leave best to find solution for my problem but no luck.

I need to keep mapping in below data structure which I want to use later in generating page from template. If I need to define below data structure in Python or Perl then I can do it easily do this.

Below is sample code for python.

{
    'abc' : {
        '1' : ['A', 'B', 'C']
    }
    'def' : {
        '1': {
            'key1':'val1',
            'key2':'val2',
            .....
            ...
            },
        '2':{
            'key1':'val1',
            'key2':'val2',
            .....
            ...
            },
        ....
        ....
    }
}

Is there any easy way to achieve the same in golang? I understand that by uses of make(map[string]map[string]string) or
make(map[string]interface{}) . But still this seems me quite unreadable.

Update 1: Just clicked the idea that I can define the same structure in JSON format as well. But still is there any other way directly at language level?

  • 写回答

1条回答 默认 最新

  • douhao1956 2016-09-12 15:50
    关注

    It is probably easier to define a type (either at the package level or within the function), but you can define literal structs inline (arrays of these are usually seen in table-driven tests).

    blah := struct{
        s string
        f int
    }{"blah",12}
    

    These can be nested arbitrarily, but it gets to be harder to read.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法