Issue in resubmiting file using F5 in go web app

Hello Guys, I am new to go language. I am getting issue when I press F5 on submitted form, It gives me following error.

2018/08/06 12:20:50 multipart: NextPart: EOF
2018/08/06 12:20:50 http: panic serving [::1]:5433: runtime error: invalid memory address or nil pointer dereference
PS D:\Golang\src\github.com\deepaksinghkushwah\upload-test> go run main.go
2018/08/06 12:25:19 multipart: NextPart: EOF
2018/08/06 12:25:19 http: panic serving [::1]:5506: runtime error: invalid memory address or nil pointer dereference
goroutine 4 [running]:
net/http.(*conn).serve.func1(0xc04216e000)
C:/Go/src/net/http/server.go:1726 +0xd7
panic(0x715740, 0x948b50)
C:/Go/src/runtime/panic.go:502 +0x237
main.uploadhandler(0x7ba1a0, 0xc042176000, 0xc04212e100)
D:/Golang/src/github.com/deepaksinghkushwah/upload-test/main.go:46 +0x29a
net/http.HandlerFunc.ServeHTTP(0x78ee00, 0x7ba1a0, 0xc042176000, 0xc04212e100)
C:/Go/src/net/http/server.go:1947 +0x4b
net/http.(*ServeMux).ServeHTTP(0x957920, 0x7ba1a0, 0xc042176000, 0xc04212e100)
C:/Go/src/net/http/server.go:2337 +0x137
net/http.serverHandler.ServeHTTP(0xc0421620d0, 0x7ba1a0, 0xc042176000, 0xc04212e100)
C:/Go/src/net/http/server.go:2694 +0xc3
net/http.(*conn).serve(0xc04216e000, 0x7ba360, 0xc042018200)
C:/Go/src/net/http/server.go:1830 +0x658
created by net/http.(*Server).Serve
C:/Go/src/net/http/server.go:2795 +0x282

Here is my code.

package main

import (
“fmt”
“html/template”
“io”
“log”
“net/http”
“os”
“strings”

github.com/satori/go.uuid
)

var tpl *template.Template

// Page struct which describe page structure
type Page struct {
Title string
Keywords string
Description string
Message string
}

func init() {
tpl = template.Must(template.ParseGlob(“templates/*.html”))
}

func homehandler(w http.ResponseWriter, r *http.Request) {
page := Page{Title: “Home Page”, Keywords: “This is home page keywords”, Description: “This is description of page”}
err := tpl.ExecuteTemplate(w, “index.html”, page)
checkWebError(w, err)
}

func uploadhandler(w http.ResponseWriter, r *http.Request) {

if r.Method == “POST” {
r.ParseMultipartForm(32 << 20)
file, handle, err := r.FormFile(“image”)
checkError(err)

  // we will use this id for new unique name
  id, err := uuid.NewV4()

  // extension will come with . (dot) extension
  ext := strings.Index(handle.Filename, ".")

  newname := fmt.Sprintf("%s", id) + handle.Filename[ext:]

  defer file.Close()
  f, err := os.OpenFile("./images/"+newname, os.O_WRONLY|os.O_CREATE, 0777)
  checkError(err)

  defer f.Close()
  io.Copy(f, file)

}
page := Page{Title: “File Uploaded”, Keywords: “This is home page keywords”, Description: “This is description of page”, Message: “File Uploaded”}
err := tpl.ExecuteTemplate(w, “index.html”, page)
checkWebError(w, err)
}

func checkWebError(w http.ResponseWriter, error error) {
if error != nil {
log.Println(error)
w.WriteHeader(http.StatusInternalServerError)
return
}
}

func checkError(error error) {
if error != nil {
log.Println(error)
return
}
}

func main() {
http.HandleFunc("/", homehandler)
http.HandleFunc("/upload", uploadhandler)
http.ListenAndServe(":8080", nil)
}

Please help me and let me know how to solve this issue.

Regards
Deepak SIngh Kushwah

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.