Go standard library has a lot of useful packages to create a webserver and it’s very well built so you can use it in production,
I will write a series of posts about the net/http package. In this tutorial I will show how to create a Hello World Webserver in Go.
Let’s start by creating a Hello World using the net/http package, create a file named main.go:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main
import (
"fmt"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello")
}
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":8080", nil)
}
Here are some exaplanations about this code:
- First we import the
fmtandnet/httppackages - We define a
handler()function that writes “Hello” to the response - Then in our
main()function we define a route that responds every request with ourhandler()function - Start our server and tell it to listen at port 8080
The handler function must receive the following parameters: an http.ResponseWriter and a pointer to http.Request, where http.ResponseWriter
is the object used to write all the data to the Response.
Now we can run our Hello World example and see if everything works as expected:
$ go run main.goAnd we can test it:
$ curl -i http://localhost:8080
HTTP/1.1 200 OK
Content-Length: 5
Content-Type: text/plain; charset=utf-8
Hello