go - 初始化第三方库的一部分结构

标签 go

我想初始化一片结构(结构的类型为 &dns.ResourceRecordSet ),其中 dns 是谷歌云 dns 库。我正在尝试这样的事情

    rr := []*gcp.ResourceRecordSet {
    }{
        &gcp.ResourceRecordSet{
            Name:    "example.gcp.com",
            Ttl:     21600,
            Type:    "NS",
            Rrdatas: []string{"ns-cloud-c1.googledomains.com.", "ns-cloud-c2.googledomains.com.", "ns-cloud-c3.googledomains.com.", "ns-cloud-c4.googledomains.com."},
        },
        &gcp.ResourceRecordSet{
            Name:    "example.gcp.com",
            Ttl:     21600,
            Type:    "NS",
            Rrdatas: []string{"ns-cloud-c1.googledomains.com.", "ns-cloud-c2.googledomains.com.", "ns-cloud-c3.googledomains.com.", "ns-cloud-c4.googledomains.com."},
        },
    }

但我收到一条错误消息,说 expected ';', found '{' on the opening braces in the second line。

正确的语法是什么?

最佳答案

你有一个额外的 }{ 你不应该有,代码应该是这样的:

rr := []*gcp.ResourceRecordSet {
    &gcp.ResourceRecordSet{
        Name:    "example.gcp.com",
        Ttl:     21600,
        Type:    "NS",
        Rrdatas: []string{"ns-cloud-c1.googledomains.com.", "ns-cloud-c2.googledomains.com.", "ns-cloud-c3.googledomains.com.", "ns-cloud-c4.googledomains.com."},
    },
    &gcp.ResourceRecordSet{
        Name:    "example.gcp.com",
        Ttl:     21600,
        Type:    "NS",
        Rrdatas: []string{"ns-cloud-c1.googledomains.com.", "ns-cloud-c2.googledomains.com.", "ns-cloud-c3.googledomains.com.", "ns-cloud-c4.googledomains.com."},
    },
}

您正在创建一个 slice 文字,其元素是结构文字(的地址)。 slice 文字看起来像:

rr := []TYPE{
  element,
  element,
}

你反而有

rr := []TYPE{
}{
  element,
  element,
}

关于go - 初始化第三方库的一部分结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57243794/

相关文章:

go - Golang 1.4.1 中 GC 导致的 cpu 核心数是多少

go - 如何在golang中获取调用者参数字符串?不可能吗?

如果包含 time.Sleep,Goroutine 不会执行

go - 《 Go编程语言》中的一个奇怪问题

go - 是否有非阻塞 http 客户端?

Golang 意外的 EOF

google-app-engine - 如何使用 Go 获取当前命名空间和 App Engine 延迟函数?

go - 如何在 Go 中的时区之间进行转换?

Go - 如何使用 http.NewRequest 进行测试

json - Go 中的函数对数据库执行选择查询并返回 json 输出