Last active
January 14, 2020 13:48
-
-
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]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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