Golang serve html files tutorial # 3
Golang serve html files tutorial # 3
we need two files.
- main.go file
- HTML file
to serve html files we use the following code:
tmpl, _ := template.ParseFiles(“index.html”)
package main import ( "fmt" "html/template" "net/http" ) func main() { tmpl, _ := template.ParseFiles("index.html") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { tmpl.Execute(w, nil) }) http.ListenAndServe(":8080", nil) }
HTML FILE
<html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>GOLANG HTML</title> </head> <body> HTML PAGE <h1>HTML PAGE</h1> <h2>HTML PAGE</h2> <h3>HTML PAGE</h3> </body> </html>