Iam getting this error, for creating a form that takes the file and perform read and write operation

this is my code

**package main

import (
“fmt”
“html/template”
“io/ioutil”
“net/http”
“os”
“path/filepath”
)

var tpl *template.Template

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

func main() {
http.HandleFunc("/", foo)
http.Handle("/favicon.ico", http.NotFoundHandler())
http.ListenAndServe(":8080", nil)
}

// check error
func errorCheck(err error, w http.ResponseWriter) {
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}

//foo call for route “/”
func foo(w http.ResponseWriter, req *http.Request) {
var s string
fmt.Println(req.Method)

if req.Method == http.MethodPost {
	//open...
	f, h, err := req.FormFile("q")
	errorCheck(err, w)

	defer f.Close()

	fmt.Println("\nfile name: ", f, "\nheader:", h, "\nerror:", err)

	bs, err := ioutil.ReadAll(f)
	errorCheck(err, w)

	s = string(bs)

	//store on server...
	dst, err := os.Create(filepath.Join("./user/", h.Filename))
	errorCheck(err, w)

	defer dst.Close()

	_, err = dst.Write(bs)
	errorCheck(err, w)

}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
tpl.ExecuteTemplate(w, "index.gohtml", s)

}
**

this is the error iam getting

***niteesh@niteesh-HP-Laptop-15-bs0xx:~/Desktop/goworkspace/src/github.com/egnimos/golang_WEB_HTTP_package/passing_data/form_file_write$ go run main.go
2020/08/01 18:03:17 http: panic serving [::1]:36012: runtime error: invalid memory address or nil pointer dereference
goroutine 19 [running]:
net/http.(*conn).serve.func1(0xc0000c0fa0)
/usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x780b80, 0xadc810)
/usr/local/go/src/runtime/panic.go:975 +0x3e3
html/template.(*Template).lookupAndEscapeTemplate(0x0, 0x7e6e17, 0xc, 0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:144 +0x4a
html/template.(*Template).ExecuteTemplate(0x0, 0x86bde0, 0xc0000fc0e0, 0x7e6e17, 0xc, 0x761160, 0xb18220, 0xc000047b78, 0x6ffdda)
/usr/local/go/src/html/template/template.go:133 +0x43
main.foo(0x871940, 0xc0000fc0e0, 0xc000118000)
/home/niteesh/Desktop/goworkspace/src/github.com/egnimos/golang_WEB_HTTP_package/passing_data/form_file_write/main.go:73 +0x23a
net/http.HandlerFunc.ServeHTTP(0x801810, 0x871940, 0xc0000fc0e0, 0xc000118000)
/usr/local/go/src/net/http/server.go:2012 +0x44
net/http.(*ServeMux).ServeHTTP(0xaec780, 0x871940, 0xc0000fc0e0, 0xc000118000)
/usr/local/go/src/net/http/server.go:2387 +0x1a5
net/http.serverHandler.ServeHTTP(0xc0000fc000, 0x871940, 0xc0000fc0e0, 0xc000118000)
/usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc0000c0fa0, 0x8722c0, 0xc0000c2300)
/usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2933 +0x35c
2020/08/01 18:03:17 http: panic serving [::1]:36014: runtime error: invalid memory address or nil pointer dereference
goroutine 20 [running]:
net/http.(*conn).serve.func1(0xc0000c1040)
/usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x780b80, 0xadc810)
/usr/local/go/src/runtime/panic.go:975 +0x3e3
html/template.(*Template).lookupAndEscapeTemplate(0x0, 0x7e6e17, 0xc, 0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:144 +0x4a
html/template.(*Template).ExecuteTemplate(0x0, 0x86bde0, 0xc00019c000, 0x7e6e17, 0xc, 0x761160, 0xb18220, 0xc00004bb78, 0x6ffdda)
/usr/local/go/src/html/template/template.go:133 +0x43
main.foo(0x871940, 0xc00019c000, 0xc00006e000)
/home/niteesh/Desktop/goworkspace/src/github.com/egnimos/golang_WEB_HTTP_package/passing_data/form_file_write/main.go:73 +0x23a
net/http.HandlerFunc.ServeHTTP(0x801810, 0x871940, 0xc00019c000, 0xc00006e000)
/usr/local/go/src/net/http/server.go:2012 +0x44
net/http.(*ServeMux).ServeHTTP(0xaec780, 0x871940, 0xc00019c000, 0xc00006e000)
/usr/local/go/src/net/http/server.go:2387 +0x1a5
net/http.serverHandler.ServeHTTP(0xc0000fc000, 0x871940, 0xc00019c000, 0xc00006e000)
/usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc0000c1040, 0x8722c0, 0xc00005e040)
/usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2933 +0x35c
2020/08/01 18:03:17 http: panic serving [::1]:36016: runtime error: invalid memory address or nil pointer dereference
goroutine 5 [running]:
net/http.(*conn).serve.func1(0xc000072000)
/usr/local/go/src/net/http/server.go:1772 +0x139
panic(0x780b80, 0xadc810)
/usr/local/go/src/runtime/panic.go:975 +0x3e3
html/template.(*Template).lookupAndEscapeTemplate(0x0, 0x7e6e17, 0xc, 0x0, 0x0, 0x0)
/usr/local/go/src/html/template/template.go:144 +0x4a
html/template.(*Template).ExecuteTemplate(0x0, 0x86bde0, 0xc00007c000, 0x7e6e17, 0xc, 0x761160, 0xb18220, 0xc00004cb78, 0x6ffdda)
/usr/local/go/src/html/template/template.go:133 +0x43
main.foo(0x871940, 0xc00007c000, 0xc00006e100)
/home/niteesh/Desktop/goworkspace/src/github.com/egnimos/golang_WEB_HTTP_package/passing_data/form_file_write/main.go:73 +0x23a
net/http.HandlerFunc.ServeHTTP(0x801810, 0x871940, 0xc00007c000, 0xc00006e100)
/usr/local/go/src/net/http/server.go:2012 +0x44
net/http.(*ServeMux).ServeHTTP(0xaec780, 0x871940, 0xc00007c000, 0xc00006e100)
/usr/local/go/src/net/http/server.go:2387 +0x1a5
net/http.serverHandler.ServeHTTP(0xc0000fc000, 0x871940, 0xc00007c000, 0xc00006e100)
/usr/local/go/src/net/http/server.go:2807 +0xa3
net/http.(*conn).serve(0xc000072000, 0x8722c0, 0xc00005e100)
/usr/local/go/src/net/http/server.go:1895 +0x86c
created by net/http.(*Server).Serve
/usr/local/go/src/net/http/server.go:2933 +0x35c


OK, this code starts a server. Which exact request to you execute against it that generates the error?

exact request ?? srry but can you clarify what you are saying

This code starts a server that listens for HTTP requests on localhost:8080. Which exact HTTP request to you execute agains http://localhost:8080/ that generates the error?

okay, so you are saying that …which function is been executed while redirecting to this

http://localhost:8080/
URL

**while *redirecting to this URL the

foo
function is been executed


this is a code of that function…

func foo(w http.ResponseWriter, req *http.Request) {
var s string
fmt.Println(req.Method)

if req.Method == http.MethodPost {
	//open...
	f, h, err := req.FormFile("q")
	errorCheck(err, w)

	defer f.Close()

	fmt.Println("\nfile name: ", f, "\nheader:", h, "\nerror:", err)

	bs, err := ioutil.ReadAll(f)
	errorCheck(err, w)

	s = string(bs)

	//store on server...
	dst, err := os.Create(filepath.Join("./user/", h.Filename))
	errorCheck(err, w)

	defer dst.Close()

	_, err = dst.Write(bs)
	errorCheck(err, w)

}

w.Header().Set("Content-Type", "text/html; charset=utf-8")
tpl.ExecuteTemplate(w, "index.gohtml", s)

}

Are you sure index.gohtml exists?

yaa man, it exists

thankuuu …so much for helping me …problem has been resolved

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