为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
白板的微信 2023-06-05 09:55:51
在 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 回答

?
隔江千里

TA贡献1906条经验 获得超10个赞

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},

}


查看完整回答
反对 回复 2023-06-05
?
胡子哥哥

TA贡献1825条经验 获得超6个赞

这就是答案。


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)


查看完整回答
反对 回复 2023-06-05
  • 2 回答
  • 0 关注
  • 122 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信