Skip to main content
edited tags; edited tags
Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
edited body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Minify and send htmlHTML response in Go

I need to have some static htmlHTML 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?

Minify and send html response in Go

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?

Minify and send HTML response in Go

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?

Source Link
twharmon
  • 313
  • 1
  • 3
  • 19

Minify and send html response in Go

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?