I try to create web application via this tutorial, but
localhost:3000 shows content of index.html instead of page. What is wrong with my code?
index.html result
main.go
package main
import (
"html/template"
"net/http"
"os"
)
var tpl = template.Must(template.ParseFiles("index.html"))
func indexHandler(w http.ResponseWriter, r *http.Request) {
tpl.Execute(w, nil)
}
func main() {
port := os.Getenv("PORT")
if port == "" {
port = "3000"
}
mux := http.NewServeMux()
mux.HandleFunc("/", indexHandler)
http.ListenAndServe(":"+port, mux)
}
index.html
<DOCTYPE>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Orders</title>
<!-- <link rel="stylesheet" href="/assets/style.css"> -->
</head>
<body>
<h1>Orders12</h1>
</body>
</html>
go version go1.22.4 windows/amd64
if I replase tmpl.Execute(w, nil)
with w.Write([]byte("<h1>HELLO W</h1>"))
- it wokrs, so server is ok. Maybe proble is in tmpl.Execute(w, nil)
As per the comments, the first line should be <!DOCTYPE html>
(playground). See MDN for more info.