I need to have some static HTML pages on a project I am making with Go. I used the http.FileServer
, but I noticed it was slower than rendering with html/templates
, and I didn't like seeing the .html
at the end of the routes. So I went with this solution:
// home.go
package pages
import "strings"
var Home string
func init() {
html := `
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<h1>Home Page</h1>
</body>
</html>
`
htmlArray := strings.Fields(html)
htmlJoined := strings.Join(htmlArray, " ")
Home = strings.Replace(htmlJoined, "> <", "><", -1)
}
and:
// main.go
package main
import (
"net/http"
"io"
"my_project/router"
"my_project/pages"
)
func main() {
router.On("GET", "/", home)
http.HandleFunc("/", router.Handle)
http.ListenAndServe(":8080", nil)
}
func home(w http.ResponseWriter, r *http.Request) {
io.WriteString(w, pages.Home)
}
This all seems to be working well. If I understand correctly, the minification takes place when the app starts, and not on each request.
Is there anything wrong with this approach?