鍍金池/ 問(wèn)答/GO/ golang invalid memory address or nil poi

golang invalid memory address or nil pointer dereference

情況是這樣的,我是golang初學(xué)者,在學(xué)習(xí)《go語(yǔ)言編程》網(wǎng)絡(luò)編程這章的時(shí)候根據(jù)書(shū)上的代碼嘗試敲了下,發(fā)現(xiàn)報(bào)錯(cuò).

Warn: panic in  0x12cfe40 runtime error: invalid memory address or nil pointer dereference

以下是服務(wù)端代碼以及報(bào)錯(cuò)信息

package main

import (
    "net/http"
    "os"
    "io"
    "io/ioutil"
    "log"
    "html/template"
    "path"
    "runtime/debug"
)
const (
    UPLOAD_DIR = "./uploads"
)

const (
    TEMPLATE_DIR = "./views"
)

const (
    ListDir = 0x0001
)
var templates = make(map[string]*template.Template)

func staticDirHandle(mux *http.ServeMux, prefix string, staticDir string, flags int) {
    mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
        file := staticDir + r.URL.Path[len(prefix)-1 :]
        if (flags & ListDir) == 0 {
            if exits := isExists(file); !exits {
                http.NotFound(w, r)
                return
            }
        }
        http.ServeFile(w, r, file)
    })
}

func init() {

    fileInfoArr, err := ioutil.ReadDir(TEMPLATE_DIR)
    if err != nil {
        panic(err)
        return
    }

    var templateName, templatePath string

    for _, fileInfo := range fileInfoArr  {
        templateName = fileInfo.Name()
        if ext := path.Ext(templateName); ext != ".html" {
            continue
        }
        templatePath = TEMPLATE_DIR + "/" + templateName
        log.Println("Loading template:", templatePath)
        t := template.Must(template.ParseFiles(templatePath))
        templates[templateName] = t
    }
}

func check(err error) {
    if err != nil {
        panic(err)
    }

}

func renderHtml(w http.ResponseWriter, tmpl string, locals map[string]interface{}) error {
    err := templates[tmpl].Execute(w, locals)
    return err
}

func uploadHandler(w http.ResponseWriter, r * http.Request)  {
    if r.Method == "GET" {

        //讀取指定模版的內(nèi)容
        if err := renderHtml(w, "upload", nil); err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
        //根據(jù)模版語(yǔ)法渲染
        return
    }

    if r.Method == "POST" {
        //return the first from the image
        f, h, err := r.FormFile("image")
        check(err)
        //h:Fileheader describes the file info
        filename := h.Filename
        //f:a interface to access the file
        defer f.Close()

        t, err := os.Create(UPLOAD_DIR + "/" + filename)
        check(err)
        defer t.Close()

        //t.writer, f.reader
        _, err = io.Copy(t, f)
        check(err)

        //redirect a new url
        //StatusFound RFC
        http.Redirect(w, r, "/view?id=" + filename, http.StatusFound)
    }
}

func viewHandler(w http.ResponseWriter, r *http.Request) {
    imageId := r.FormValue("id")
    imagePath := UPLOAD_DIR + "/" + imageId
    if exits := isExists(imagePath); !exits {
        http.NotFound(w, r)
        return
    }
    w.Header().Set("Content-Type", "image")
    http.ServeFile(w, r, imagePath)
}

func isExists(path string) bool {
    _, err := os.Stat(path)
    if err == nil {
        return true
    }
    return os.IsExist(err)
}

func listHandler(w http.ResponseWriter, r *http.Request)  {
    fileInfoArr, err := ioutil.ReadDir("./uploads")
    check(err)
    locals := make(map[string] interface{})
    var images []string


    for _, fileInfo := range fileInfoArr {
        images = append(images, fileInfo.Name())
    }

    locals["images"] = images
    err = renderHtml(w, "list", locals)
    check(err)
}

func safeHandler(fn http.HandlerFunc) http.HandlerFunc {
    return func(w http.ResponseWriter, r *http.Request) {
        defer func() {
            if e, ok := recover().(error); ok {
                http.Error(w, e.Error(),http.StatusInternalServerError)
                log.Println("Warn: panic in ", fn, e)
                log.Println(string(debug.Stack()))
            }
        }()
        fn(w, r)
    }
}



func main() {

    mux := http.NewServeMux()
    staticDirHandle(mux, "/assets/", "./public", 0)
    http.HandleFunc("/", safeHandler(listHandler))
    http.HandleFunc("/view", safeHandler(viewHandler))
    http.HandleFunc("/upload", safeHandler(uploadHandler))
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        log.Fatal("listenAndServe: ", err.Error())
    }
}

圖片描述

請(qǐng)求各位能幫我解答疑惑,十分感謝。

回答
編輯回答
瞄小懶

這一行

 err := templates[tmpl].Execute(w, locals)

要先檢查map里面有沒(méi)有

t, ok := templates[tmpl]
if !ok {
    return errors.New("沒(méi)有找到模板")
}
err := t.Execute(w, locals)
return err
2018年8月3日 02:51
編輯回答
陪我終

這是一個(gè)很明顯的錯(cuò)誤,你稍微 debug 一下就很明了了。

提示一點(diǎn):從你的 templates map 中沒(méi)有找到你傳遞的模板名,而 go 中的 map 索引如果沒(méi)有找到并不會(huì)報(bào)錯(cuò),而是返回一個(gè)該類(lèi)型的默認(rèn)值

2017年9月25日 07:51