我想初始化一片结构(结构的类型为 &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/