The http package and building a server.

Zachary Endrulat
4 min readJul 1, 2018

--

The http package is really big but I am going to attempt to break down some parts starting with the Reader and Writer types.

We will start with the most used portion of the package. The handlers are what are in all servers.

https://golang.org/pkg/net/http/#HandleFunc

First we need to have control over the server and get it going. The ListenAndServe you can think of as what you are going to use to set the port (:8080) and if you are going to pass in data.

https://golang.org/pkg/net/http/#ListenAndServe

The ListenAndServeTLS is used for passing certificates usually used in hosting for security and other reasons. But here is its use from the docs. Please read it because the whole point of the docs and this article is to provide a mental model of the code instead of copy, paste, and forget.

https://golang.org/pkg/net/http/#ListenAndServeTLS

The http.HandleFunc is used to supply the route url or the first segment of the url to usually serve files.

But we have to understand the types it utilizes like the handle type and the types that are integrated into the Handler interface.

https://golang.org/pkg/net/http/#Handler

The type ResponseWriter interface has the Write type that we learned before.

Write([]byte) (int, error)

https://golang.org/pkg/net/http/#ResponseWriter

The type Request is used to represent the request and all the data that represents a request (url, header, body..ect).

https://golang.org/pkg/net/http/#Request

This is all important to understand to have a mental model when trying to understand the code. I used to just copy paste the example from the docs and think well thats what is in the documentation but doing so leaves out further options and an understanding of the code you are writing. You have to ask yourself, “can you test this code?”.

Now that we have an understanding of the type Handler we can now use the HandleFunc. It is relatively easy to use.

https://golang.org/pkg/net/http/#HandleFunc

You basically setup the segment url that you want the file to be served on with the name of the function that is going to serve it. In the doc example the url on your domain is “/hello” and the function is HelloServer.

The function however must take in the http.ResponseWriter and *http.Request which are utilized by the HandleFunc.

Usually though in this request a template file is served or an api. You would just put it in the function like HelloServer.

I will go over this in the next article for templates because that is a big package as well and for reference sake I am splitting it up. I didnt really cover the http package very much because its pretty big. I dont think anyone would read such an article either. I hope this explained the basic use of the most popular methods of the http package. This is the base structure of a web server without a framework, 3rd party package, or an environment software like npm. Anyone who has used django or express can surely appreciate this simplicity and ease of use. You are starting to see that these package types are getting complex and sorta have some magic hidden. In the http package there is a lot of protocals and server heavy lifting. Things that I believe bleeds more into IT.

--

--