Skip to content

Instantly share code, notes, and snippets.

@ninetails
Forked from superbrothers/app.go
Created March 2, 2017 22:37
Show Gist options
  • Save ninetails/6cde9d1c72405ad4a8ad9f0882ad1202 to your computer and use it in GitHub Desktop.
Save ninetails/6cde9d1c72405ad4a8ad9f0882ad1202 to your computer and use it in GitHub Desktop.
Simple Web Server with golang
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func rootHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
data, err := ioutil.ReadFile("index.html")
if err != nil {
panic(err)
}
w.Header().Set("Content-Length", fmt.Sprint(len(data)))
fmt.Fprint(w, string(data))
}
func main() {
http.HandleFunc("/", rootHandler)
log.Fatal(http.ListenAndServe(":8080", nil))
}
<h1>Hello world</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment