如何在golang中重新创建perl的哈希数组?

时间:2019-01-29 14:13:20

标签: perl go

在perl中,我可以将$ hashref推送到@array中,并将此数据用于下一个foreach和可能的encode_json(HTTP POST)。

我不知道如何在golang中重新创建相同的登录名?

$VAR1 = [
      {
        'address' => 'test.com',
        'id' => 101,
        'hostgroups' => [
                          zero
                          'one',
                          'or many'
                        ],
        'host_name' => 'test.com',
        'alias' => 'test.com',
        'template' => 'generic',
        'file_id' => 'etc/config'
      },
      {
        'address' => 'test2.com',
        'id' => 102,
        'hostgroups' => [
                          zero
                          'one',
                          'or many'
                        ],
        'host_name' => 'test2.com',
        'alias' => 'test2.com',
        'template' => 'generic',
        'file_id' => 'etc/config'
      },
      (..)

2 个答案:

答案 0 :(得分:1)

var array = []map[string]interface{}{
    {"address": "test.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 101},
    {"address": "test2.com", "hostgroups": []string{"zero", "one", "or many"}, "id": 102},
}

答案 1 :(得分:0)

这就是答案。

type host map[string]interface{}

var hosts []host

h := host{
    "id":         id,
    "file_id":    "etc/config/hosts.cfg",
    "host_name":  host_name,
    "alias":      host_name,
    "address":    host_name,
    "hostgroups": hg,
    "template":   "generic-host",
}

hosts = append(hosts, h)