Saturday, March 26, 2016

Set HTTP header content type to HTML in Go Gorilla

package main

import (
    "fmt"
    "net/http"
    "log"
    "github.com/gorilla/mux"
)

const (
    PORT       = ":80"
)

func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "text/html; charset=utf-8")
    w.Write([]byte("<span style=\"color: red;\">Hello, World!</span>"))
}

func main() {
    mx := mux.NewRouter()
    mx.HandleFunc("/", SayHelloWorld)

    err := http.ListenAndServe(PORT, mx)

    if err != nil {
        fmt.Printf("main(): %s\n", err)
        log.Fatal("ListenAndServe: ", err)
    }
}

Reference:

http://stackoverflow.com/questions/12830095/setting-http-headers-in-golang

No comments: