go - 收到 EOF panic 错误

标签 go

我正在尝试解码我​​得到的 json。这是我得到的示例 json:

{"response":"1","number":"1234","id":nil}

这是我的结构:

type AutoGenerated struct {
Response string  `json:"response"`
Number     string  `json:"number"`
ID         interface{}     `json:"id"`
}

我在encode/json中使用decode函数。我错了什么? ID 有可能既是字符串也可能是 nil 值。

这是我的确切错误,以防有帮助。

panic: EOF

最佳答案

如果您不展示您是如何做的,我认为最好的答案是向您展示如何做。

package main

import (
    "fmt"
    "log"
    "encoding/json"
)

func main() {
    j := []byte(`{"response":"1","number":"1234","id":null}`)
    data := AutoGenerated{}
    err := json.Unmarshal(j, &data)
    if err != nil {
        log.Println(err.Error())
        return
    }
    fmt.Println(data)
}

type AutoGenerated struct {
    Response string      `json:"response"`
    Number   string      `json:"number"`
    ID       interface{} `json:"id"`
}

关于go - 收到 EOF panic 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39650410/

相关文章:

在文件更改时自动重新编译和重新加载服务器

string - 在 Go 中拆分字符串列表中的字符串

Go gzip 没有输出预期的输出

sql - gorp: "auto_increment"附近:语法错误

在 golang 中排序

go - 我如何遍历解码器?

go - 尽管选项响应中出现“Access-Control-Allow-Origin” header ,stilll却出现cors错误

go - 如何使用 Go 接口(interface)来测试 Google API Golang 库?

go - 如何使用 r3labs/diff 比较整个结构

if-statement - GoLang,如何编写多行if条件语句