내 Go 프로그램을 실행할 때 패닉이 발생하고 다음을 반환합니다.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x38 pc=0x26df]
goroutine 1 [running]:
main.getBody(0x1cdcd4, 0xf800000004, 0x1f2b44, 0x23, 0xf84005c800, ...)
/Users/matt/Dropbox/code/go/scripts/cron/fido.go:65 +0x2bb
main.getToken(0xf84005c7e0, 0x10)
/Users/matt/Dropbox/code/go/scripts/cron/fido.go:140 +0x156
main.main()
/Users/matt/Dropbox/code/go/scripts/cron/fido.go:178 +0x61
goroutine 2 [syscall]:
created by runtime.main
/usr/local/Cellar/go/1.0.3/src/pkg/runtime/proc.c:221
goroutine 3 [syscall]:
syscall.Syscall6()
/usr/local/Cellar/go/1.0.3/src/pkg/syscall/asm_darwin_amd64.s:38 +0x5
syscall.kevent(0x6, 0x0, 0x0, 0xf840085188, 0xa, ...)
/usr/local/Cellar/go/1.0.3/src/pkg/syscall/zsyscall_darwin_amd64.go:199 +0x88
syscall.Kevent(0xf800000006, 0x0, 0x0, 0xf840085188, 0xa0000000a, ...)
/usr/local/Cellar/go/1.0.3/src/pkg/syscall/syscall_bsd.go:546 +0xa4
net.(*pollster).WaitFD(0xf840085180, 0xf840059040, 0x0, 0x0, 0x0, ...)
/usr/local/Cellar/go/1.0.3/src/pkg/net/fd_darwin.go:96 +0x185
net.(*pollServer).Run(0xf840059040, 0x0)
/usr/local/Cellar/go/1.0.3/src/pkg/net/fd.go:236 +0xe4
created by net.newPollServer
/usr/local/Cellar/go/1.0.3/src/pkg/net/newpollserver.go:35 +0x382
다른 사람들이 동일한 예외에 대해 가졌던 응답을 살펴 봤지만 간단한 것은 볼 수 없습니다 (예 : 처리되지 않은 오류).
코드에 나열된 API 서버에 대한 액세스 권한이없는 컴퓨터에서 실행 중이지만 적절한 오류를 반환하기를 바라고 있습니다.
package main
/*
Fido fetches the list of public images from the Glance server, captures the IDs of images with 'status': 'active' and then queues the images for pre-fetching with the Glance CLI utility `glance-cache-manage`. Once the images are added to the queue, `glance-cache-prefetcher` is called to actively fetch the queued images into the local compute nodes' image cache.
See http://docs.openstack.org/developer/glance/cache.html for further details on the Glance image cache.
*/
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
/*
"log"
"log/syslog"
*/
"net/http"
"os"
"os/exec"
)
func prefetchImages() error {
cmd := exec.Command("glance-cache-prefetcher")
err := cmd.Run()
if err != nil {
return fmt.Errorf("glance-cache-prefetcher failed to execute properly: %v", err)
}
return nil
}
func queueImages(hostname string, imageList []string) error {
for _, image := range imageList {
cmd := exec.Command("glance-cache-manage", "--host=", hostname, "queue-image", image)
err := cmd.Run()
if err != nil {
return fmt.Errorf("glance-cache-manage failed to execute properly: %v", err)
} else {
fmt.Printf("Image %s queued", image)
}
}
return nil
}
func getBody(method string, url string, headers map[string]string, body []byte) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest(method, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Add(key, value)
}
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return nil, err
}
var bodyBytes []byte
if res.StatusCode == 200 {
bodyBytes, err = ioutil.ReadAll(res.Body)
} else if err != nil {
return nil, err
} else {
return nil, fmt.Errorf("The remote end did not return a HTTP 200 (OK) response.")
}
return bodyBytes, nil
}
func getImages(authToken string) ([]string, error) {
type GlanceDetailResponse struct {
Images []struct {
Name string `json:"name"`
Status string `json:"status"`
ID string `json:"id"`
}
}
method := "GET"
url := "http://192.168.1.2:9292/v1.1/images/detail"
headers := map[string]string{"X-Auth-Token": authToken}
bodyBytes, err := getBody(method, url, headers, nil)
if err != nil {
return nil, fmt.Errorf("unable to retrieve the response body from the Glance API server: %v", err)
}
var glance GlanceDetailResponse
err = json.Unmarshal(bodyBytes, &glance)
if err != nil {
return nil, fmt.Errorf("unable to parse the JSON response:", err)
}
imageList := make([]string, 10)
for _, image := range glance.Images {
if image.Status == "active" {
imageList = append(imageList, image.ID)
}
}
return imageList, nil
}
func getToken() (string, error) {
type TokenResponse struct {
Auth []struct {
Token struct {
Expires string `json:"expires"`
ID string `json:"id"`
}
}
}
method := "POST"
url := "http://192.168.1.2:5000/v2.0/tokens"
headers := map[string]string{"Content-type": "application/json"}
creds := []byte(`{"auth":{"passwordCredentials":{"username": "glance", "password":"<password>"}, "tenantId":"<tenantkeygoeshere>"}}`)
bodyBytes, err := getBody(method, url, headers, creds)
if err != nil {
return "", err
}
var keystone TokenResponse
err = json.Unmarshal(bodyBytes, &keystone)
if err != nil {
return "", err
}
authToken := string((keystone.Auth[0].Token.ID))
return authToken, nil
}
func main() {
/*
slog, err := syslog.New(syslog.LOG_ERR, "[fido]")
if err != nil {
log.Fatalf("unable to connect to syslog: %v", err)
os.Exit(1)
} else {
defer slog.Close()
}
*/
hostname, err := os.Hostname()
if err != nil {
// slog.Err("Hostname not captured")
os.Exit(1)
}
authToken, err := getToken()
if err != nil {
// slog.Err("The authentication token from the Glance API server was not retrieved")
os.Exit(1)
}
imageList, err := getImages(authToken)
err = queueImages(hostname, imageList)
if err != nil {
// slog.Err("Could not queue the images for pre-fetching")
os.Exit(1)
}
err = prefetchImages()
if err != nil {
// slog.Err("Could not queue the images for pre-fetching")
os.Exit(1)
}
return
}
답변
문서에 따르면 func (*Client) Do
:
“클라이언트 정책 (예 : CheckRedirect)으로 인해 오류가 발생하거나 HTTP 프로토콜 오류가 발생한 경우 오류가 반환됩니다. 2xx가 아닌 응답은 오류를 일으키지 않습니다.
err이 nil이면 resp는 항상 nil이 아닌 resp.Body를 포함합니다. “
그런 다음이 코드를 살펴보십시오.
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return nil, err
}
나는 err
그렇지 않다고 생각한다 nil
. 을 확인하기 전에 의 .Close()
메서드에 액세스하고 res.Body
있습니다 err
.
는 defer
단지 함수 호출을 지연시킵니다. 필드와 방법은 즉시 액세스됩니다.
따라서 대신 즉시 오류를 확인하십시오.
res, err := client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
답변
nil 포인터 역 참조는 65 행에 있습니다.
res, err := client.Do(req)
defer res.Body.Close()
if err != nil {
return nil, err
}
err! = nil이면 res == nil이고 res.Body 패닉. res.Body.Close ()를 연기하기 전에 err을 처리하십시오.
답변
내 문제로 여기에 왔기 때문에 원래 질문과 정확히 관련이 없지만이 답변을 추가하겠습니다. 인터페이스를 구현할 때 멤버 함수 선언에 유형 포인터를 추가하는 것을 잊지 마십시오. 예:
type AnimalSounder interface {
MakeNoise()
}
type Dog struct {
Name string
mean bool
BarkStrength int
}
func (dog *Dog) MakeNoise() {
//implementation
}
* (dog Dog) 부분을 잊어 버렸습니다 . 추천하지 않습니다. 그런 다음 Dog 유형의 AnimalSounder 인터페이스 변수에서 MakeNoice를 호출 할 때 추악한 문제가 발생합니다.
답변
나 에게이 문제에 대한 한 가지 해결책은 sql.Open … sslmode = disable을 추가하는 것이 었습니다.
답변
