在这个问题上敲我的头。我无法获得分配给结构的 HTTP 响应。
我的结构是这样设置的:
type DataConnect struct {
Response *Response
}
type Response struct {
response []byte
errors []string
}
然后有问题的函数布局如下(为了便于阅读而修剪):
137 func (d *DataConnect) send() bool {
...
154 out, err := ioutil.ReadAll(resp.Body)
155 if err != nil {
156 fmt.Println(err)
157 }
158
159 fmt.Printf("%s\n", out) // THIS WORKS
160 d.Response.response = out // THIS DOES NOT WORK
161 }
这样做会导致以下错误:
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x0 pc=0x36532]
goroutine 1 [running]:
github.com/DataConnect.(*DataConnect).send(0xc2000af4a0, 0x232a00)
github.com/DataConnect/DataConnect.go:160 +0xc22
现在,如果我将 DataConnect.Response.response
更改为键入 interface{}
,我可以成功保存到它,但是我需要它在 []byte
稍后我将对内容执行 json.Unmarshal。
有人知道为什么这不起作用吗?
最佳答案
我怀疑第 160 行的 d 为 nil 或 d.Response 为 nil。如果是这样,您需要确定这是否合适,如果不合适则更改您的代码。
关于google-app-engine - 解决无效的内存地址或 nil 指针取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18752544/