解决无效的内存地址或空指针解引用问题

huangapple go评论113阅读模式
英文:

Resolving an invalid memory address or nil pointer dereference

问题

我正在为你翻译以下内容:

我在这个问题上碰壁了。我无法将HTTP响应分配给一个结构体。

我设置了如下的结构体:

  1. type DataConnect struct {
  2. Response *Response
  3. }
  4. type Response struct {
  5. response []byte
  6. errors []string
  7. }

然后,相关的函数如下所示(为了可读性进行了简化):

  1. func (d *DataConnect) send() bool {
  2. ...
  3. out, err := ioutil.ReadAll(resp.Body)
  4. if err != nil {
  5. fmt.Println(err)
  6. }
  7. fmt.Printf("%s\n", out) // 这个可以工作
  8. d.Response.response = out // 这个不工作
  9. }

执行上述代码会导致以下错误:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xb code=0x1 addr=0x0 pc=0x36532]
  3. goroutine 1 [running]:
  4. github.com/DataConnect.(*DataConnect).send(0xc2000af4a0, 0x232a00)
  5. github.com/DataConnect/DataConnect.go:160 +0xc22

如果我将DataConnect.Response.response的类型更改为interface{},我可以成功保存它,但是我需要它的类型为[]byte,因为后面我将对内容进行json.Unmarshal操作。

有人知道为什么这个不起作用吗?

英文:

Banging my head on this one. I can't get an HTTP Response to assign to a struct.

I have my structs set up like so:

  1. type DataConnect struct {
  2. Response *Response
  3. }
  4. type Response struct {
  5. response []byte
  6. errors []string
  7. }

Then the function in question is laid out like so (trimmed for readability):

  1. 137 func (d *DataConnect) send() bool {
  2. ...
  3. 154 out, err := ioutil.ReadAll(resp.Body)
  4. 155 if err != nil {
  5. 156 fmt.Println(err)
  6. 157 }
  7. 158
  8. 159 fmt.Printf("%s\n", out) // THIS WORKS
  9. 160 d.Response.response = out // THIS DOES NOT WORK
  10. 161 }

Doing that results in the following error:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. [signal 0xb code=0x1 addr=0x0 pc=0x36532]
  3. goroutine 1 [running]:
  4. github.com/DataConnect.(*DataConnect).send(0xc2000af4a0, 0x232a00)
  5. github.com/DataConnect/DataConnect.go:160 +0xc22

Now if I change DataConnect.Response.response to type interface{} I can save successfully to it, however I need it in []byte as later I will be doing a json.Unmarshal on the content.

Does anybody have any idea why this isn't working?

答案1

得分: 2

我怀疑在第160行,d要么是空值,要么d.Response是空值。如果是这样的话,你需要决定是否合适,并在不合适的情况下更改你的代码。

英文:

I suspect either d is nil or d.Response is nil on line 160. If that is true, you need to decide if that is appropriate and change your code if it is not.

答案2

得分: 1

我怀疑@alex是正确的,将你的代码更改为查找nil的内容(从第159行开始):

  1. fmt.Printf("%s\n", out) // 这个是有效的
  2. if d != nil && d.Response != nil {
  3. d.Response.response = out // 这个是无效的
  4. } else {
  5. // 适当的错误记录和处理
  6. }
英文:

I suspect @alex is correct, change your code to something that looks for nil (from line 159):

  1. fmt.Printf("%s\n", out) // THIS WORKS
  2. if d != nil && d.Response != nil {
  3. d.Response.response = out // THIS DOES NOT WORK
  4. } else {
  5. // appropriate error logging and handling
  6. }

huangapple
  • 本文由 发表于 2013年9月12日 06:46:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/18752544.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定