Home > Software engineering >  My code in golang show me the same error EMPTY RESPONSE in me browser
My code in golang show me the same error EMPTY RESPONSE in me browser

Time:04-18

package main

import (
    "fmt"
    "html/template"
    "net/http"
)

const portNumber = ":8080"

//Home is the home page handler

func Home(w http.ResponseWriter, r *http.Request) {
    renderTemplate(w, "home.page.html")
    fmt.Fprintf(w, "This is the home page ")

}

//About is the about page handler

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

}

func renderTemplate(w http.ResponseWriter, html string) {
    parsedTemplate, _ := template.ParseFiles("./template/"   html)
    
    err := parsedTemplate.Execute(w, nil)
    if err != nil {
        fmt.Println("error parsing template:", err)
        
    }
}

//main is the main application function

func main() {
    http.HandleFunc("/home", Home)
    http.HandleFunc("/about", About)
    fmt.Printf("Starting application on port %s", portNumber)
    //fmt.Sprintf("Starting application on port %s", portNumber)
    _ = http.ListenAndServe(portNumber, nil)
}

and when i try to refresh the page in my browser, in my terminal i get this, I am using Visual studio

**PS C:\Users\Lenovo\Documents\Golang\App web> go run main.go
Starting application on port :80802022/04/17 12:17:46 http: panic serving [::1]:65173: runtime error: invalid memory address or nil pointer dereference
goroutine 35 [running]:
net/http.(*conn).serve.func1()
        C:/Program Files/Go/src/net/http/server.go:1825  0xbf
panic({0xd48460, 0xff2480})
        C:/Program Files/Go/src/runtime/panic.go:844  0x258
html/template.(*Template).escape(0x0)
        C:/Program Files/Go/src/html/template/template.go:97  0x34
html/template.(*Template).Execute(0x0, {0xe1fa20, 0xc000220000}, {0x0, 0x0})**
 

And a lot of information like that

CodePudding user response:

It worked perfectly for me. However, I would recommend that you double-check your html file. This is the html I used and worked with, as well as the folder structure I used.

<!doctype html>
<html>
<h1>Hello, World</h1>
</html>

directory where the files are

site

Hope this helps you!

CodePudding user response:

I just saw my mistake, i didnt named my folder in the right way. Thanks for the help!

  • Related