dream_life5200 2017-12-27 20:38
浏览 74
已采纳

Golang恐慌:运行时错误:无效的内存地址或nil指针取消引用

i'm new to golang and it may be a very basic thing but i can't seems to find the solution.

the request return json which is like this.

{"uuid":"a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a22","name":"core1","owner":"systems","description":"new","creation_date":"2017-06-10T14:20:00Z"}

This is the gocode.

package main

import (
    "crypto/tls"
    "encoding/json"
    "fmt"
    "io/ioutil"
    "net/http"
)

type Project struct {
    Uuid          string `json:"uuid"`
    Name          string `json:"name"`
    Owner         string `json:"owner"`
    Description   string `json:"description"`
    Creation_date string `json:"creation_date"`
}

func main() {
    tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
    }
    client := &http.Client{Transport: tr}
    req, err := http.NewRequest("GET", "https://localhost:4443/project/core1", nil)
    req.SetBasicAuth("rohit", "rohit")
    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("server not responding %s", err.Error())
    }
    var p Project

    b, err := ioutil.ReadAll(resp.Body)
    defer resp.Body.Close()
    err = json.Unmarshal(b, &p)
    if err != nil {
        fmt.Printf("Test case failed with error %s", err.Error())
    }
    if resp.StatusCode != 403 {
        fmt.Printf("failed %s", err.Error())
    }
}

after running i'm getting this error

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x40142f]

goroutine 1 [running]:
panic(0x60c860, 0xc42000c130)
    /usr/local/go/src/runtime/panic.go:500 +0x1a1
main.main()
    /home/rohitk/Go_projects/src/first_program/test/main.go:41 +0x42f
exit status 2

i checked and response body has right data. can someone please suggest what's happening here.Thanks!

  • 写回答

2条回答 默认 最新

  • dormbaf90464 2017-12-27 21:40
    关注

    As mentioned by commenters, your code is only printing errors, not handling them by altering the behavior of the program.

    resp, err := client.Do(req)
    if err != nil {
        fmt.Printf("server not responding %s", err.Error())
    }
    // ...
    
    b, err := ioutil.ReadAll(resp.Body)
    

    In the snippet above, if there was an error then it gets printed; however, flow control proceeds as usual even though the "resp" object is probably not valid (e.g. nil).

    When a library program encounters an error you should usually return it immediately without any further action. For end-user applications, you should usually display the error (typically on the stderr stream) and exit the program (typically with a nonzero exit code). For example:

    resp, err := client.Do(req)
    if err != nil {
        fmt.Fprintf(os.Stderr, "ERROR: %s
    ", err)
        os.Exit(1) // Exit the program if we didn't get a response.
    }
    // ...
    
    b, err := ioutil.ReadAll(resp.Body)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?