【问题标题】:json: cannot unmarshal number 5088060241 into struct of type intjson:无法将数字 5088060241 解组为 int 类型的结构
【发布时间】:2019-11-25 20:51:03
【问题描述】:

我正在使用 OVH 提供程序处理 Terraform 项目,创建记录时,提供程序无法获取记录的 ID 并触发此错误: cannot unmarshal number 5088060240 into Go struct field OvhDomainZoneRecord.id of type int

我在 github 存储库上打开了一个问题,但仍在等待答案。 我想自己纠正这个问题,但我不是 Go 开发人员,我找不到任何相关的错误。

OvhDomainZoneRecord 的结构体:

type OvhDomainZoneRecord struct {
    Id        int    `json:"id,omitempty"`
    Zone      string `json:"zone,omitempty"`
    Target    string `json:"target"`
    Ttl       int    `json:"ttl,omitempty"`
    FieldType string `json:"fieldType"`
    SubDomain string `json:"subDomain,omitempty"`
}

相关文件: https://github.com/terraform-providers/terraform-provider-ovh/blob/master/ovh/resource_ovh_domain_zone_record.go

【问题讨论】:

标签: json go integer terraform


【解决方案1】:

int 的大小是 32 位或 64 位,具体取决于您编译和运行的目标架构。您的输入 5088060240 大于 32 位整数的最大值(即 2147483647),因此如果您的 int 为 32 位,则会出现此错误。

最简单的解决方法是使用int64。看这个例子:

var i int32
fmt.Println(json.Unmarshal([]byte("5088060240"), &i))

var j int64
fmt.Println(json.Unmarshal([]byte("5088060240"), &j))

输出(在Go Playground上试试):

json: cannot unmarshal number 5088060240 into Go value of type int32
<nil>

【讨论】:

  • 感谢您的快速回答,我将分叉项目并进行更改
  • @MartinPaucot 或使用 64 位目标(如果您愿意的话),但是是的,如果您可能得到这么大的数字,最好明确说明整数大小。
  • 我将使用 int64,这是一个临时更改。正如我所说,我真的不懂围棋。只是为了等待真正的修复
猜你喜欢
  • 2020-04-29
  • 2019-03-28
  • 2020-10-14
  • 1970-01-01
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
相关资源
最近更新 更多