Skip to content

Instantly share code, notes, and snippets.

@mtilson
Last active January 14, 2020 13:48
Show Gist options
  • Save mtilson/9172b045a568f9260f39af5e861a67cf to your computer and use it in GitHub Desktop.
Save mtilson/9172b045a568f9260f39af5e861a67cf to your computer and use it in GitHub Desktop.
how to create pure http server - in less than 20 code lines [golang] [20lines]
package main
import (
"net/http"
)
const (
jsonString = `{"hello": "world", "with_love": "from_go"}`
)
func handler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/json")
w.Write([]byte(jsonString))
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8765", nil)
}
$ go run main.go &
[1] 56789
$ curl -s http://localhost:8765 | jq
{
"hello": "world",
"with_love": "from_go"
}
$ kill %1
[1] + 56789 terminated go run main.go
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment