在Go(golang)中嵌入结构化数据

huangapple go评论78阅读模式
英文:

Embedding structured data in Go (golang)

问题

我正在使用golang开发一个AI游戏引擎,我需要将一些预计算的数据存储起来,以便尽快访问。

数据的结构如下:

{
'type1': {
0: {
0: { 1, 2, 3, 4, 5, 6, 7 },
1: { 2, 3, 4 },
},
1: {
0: { 2, 3, 4, 5, 6 },
},
},
"type2": {
0: {
{ 63, 23, 42, 12 },
},
},
}

将这些数据嵌入到编译后的二进制文件中,有什么最佳方法?这些数据永远不会改变,代表了游戏的规则。

我知道可以创建一个在堆上分配map的函数,但我认为直接在数据所在的位置直接访问数据更自然一些。

英文:

I'm working on an AI game engine in golang, and I need to store some precomputed data to be accessible as fast as possible.

The structure of the data is like this:

{
    'type1': {
        0: {
            0: { 1, 2, 3, 4, 5, 6, 7 },
            1: { 2, 3, 4 },
        },
        1: {
            0: { 2, 3, 4, 5, 6 },
        },
    },
    "type2": {
        0: {
            { 63, 23, 42, 12 },
        },
    },
}

What is the best way to embed this data into the compiled binary? The data will never change and represents the rules of the game.

I am aware that I can create a function that allocates the map on the heap, but I think it is more natural that the data is accessed directly where it is.

答案1

得分: 3

原来嵌入数据是相当容易的。要在包范围内声明一个变量:

var someVar = map[string][][][]uint8 {
    'type1': [][][]uint8 {
        0: [][]uint8 {
            0: []uint8 { 1, 2, 3, 4, 5, 6, 7, },
        },
    },
}

在包范围内声明它可以使其可访问,至少对于包范围内的函数来说。

这种标记的唯一烦人之处在于你最初必须声明整个结构的格式。然后,当你深入到层次结构中时,你需要“弹出”结构的第一部分。例如:

var someVar = map[string][][][]uint8 {}

这里,map[string]uint8 声明了一个具有字符串键和uint8值的映射。map[string][]uint8 声明了一个具有字符串键和uint8数组值的映射。

对于你深入到结构的每个级别,你都要弹出格式定义的开头。

var someVar map[string][][][]uint8 {
                    0: [][][]uint8 {
                      0: [][]uint8 {
                        0: []uint8 {
                          1, 2, 3, 4, 5,
                        },
                      },
                    },
}

奇怪的缩进是为了说明已经弹出的部分

英文:

Turns out it is quite easy to embed data. To declare a variable in a package scope:

var someVar = map[string][][][]uint8 {
    'type1': [][][]uint8 {
        0: [][]uint8 {
            0: []uint8 { 1, 2, 3, 4, 5, 6, 7, },
        },
    },
}

Declaring it in the package scope makes it accessible, at least for functions in the package scope.

The only annoying thing about this markup is that you initially have to declare the format of the entire structure. Then you need to "pop off" the first part of the structure as you go deeper into your hierarchy. Example:

var someVar = map[string][][][]uint8 {}

Here, map[string]uint8 declares a map with string keys and uint8 values. map[string][]uint8 declares a map with string keys and array of uint8 values.

For every level you go deeper into the structure, you pop off the start of the format definition.

var someVar map[string][][][]uint8 {
                    0: [][][]uint8 {
                      0: [][]uint8 {
                        0: []uint8 {
                          1, 2, 3, 4, 5,
                        },
                      },
                    },
}

The weird indentation is intended to illustrate what's been popped off

huangapple
  • 本文由 发表于 2013年12月10日 20:06:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/20494079.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定