Making a basic HTTP server with Golang
Tags: go, webdev, beginners, tutorial
Published: 2023-03-30 21:32:04


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5h2sv6fuemh21gpmr5e.png)

Golang is widely used for building web applications. The ease of spinning up a HTTP server is one of its key features and today I'm going to show you just how easy it is to spin one up in a few lines. Then register some paths that will show some strings let's get started!

<small>Finished code:</small>
```go
package main

import (
	"fmt"      // formatting and printing values to the console.
	"log"      // logging messages to the console.
	"net/http" // Used for build HTTP servers and clients.
)

// Port we listen on.
const portNum string = ":8080"

// Handler functions.
func Home(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Homepage")
}

func Info(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Info page")
}

func main() {
	log.Println("Starting our simple http server.")

	// Registering our handler functions, and creating paths.
	http.HandleFunc("/", Home)
	http.HandleFunc("/info", Info)

	log.Println("Started on port", portNum)
	fmt.Println("To close connection CTRL+C :-)")

	// Spinning up the server.
	err := http.ListenAndServe(portNum, nil)
	if err != nil {
		log.Fatal(err)
	}
}
```
---
## Required packages

For this we could really use just two packages `fmt` and `net/http` known as (`http`), but I enjoy having `log` to print certain things out in my code. If you aren't familar with these packages I've commented there use case next to them.
```go
import (
	"fmt"      // formatting and printing values to the console.
	"log"      // logging messages to the console.
	"net/http" // Used for build HTTP servers and clients.
)
```
---

## Defining handler functions
Handler functions by name will handle incoming HTTP requests. These functions will take two arguments `http.ResponseWriter` which we call `w` and `r` a pointer to a `http.Request` this is standard for declaring a handler function.

<small>Anatomy of a handler function:</small>
```go
// handlerFunc handles incoming HTTP requests.
func handlerFunc(w http.ResponseWriter, r *http.Request) {
	// Message here to the response writer.
}
```

So what are these two arguments? when we make a request to a HTTP server, the server must process this and send back a response back to the client. Our `http.ResponseWriter` is used to write this response back to the client, and the `http.Request` object contains information about the incoming request.

So filling in our handler functions with some data now we write strings that will appear on our pages saying `Homepage` and `Info`. This is where you would pass your HTML templates if we were creating a web app. For now we just want to see our strings appear on the page.
```go
func Home(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Homepage")
}

func Info(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "Info")
}
```
---

## Handling the func
Now to handle the functions we just created we use the `http.HandleFunc` function. this function will take two arguments, a path as a string and a handler function which we just created above. 

<small>The basic pattern of a HandleFunc is below:</small>
```go
// HandleFunc is used to register a handler function for a specific path on your HTTP server.
http.HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))
```

Now let's register some paths with the handler functions we created earlier.

```go
// Keeping it simple we will place them in our main function.
func main(){

	http.HandleFunc("/", Home)
	http.HandleFunc("/info", Info)
    }
```
We've now created two paths by registering our handler functions to `/`for our homepage and `/info` for our info page. Let's now learn how we can spin up our server on our chosen port.
---
## Can you hear me?

The last part is spinning up our server and listening. Listening in terms of network programming refers to the process of waiting for incoming network connections. Using the `http.ListenAndServe` function we can start a HTTP server and "listen" for incoming requests on a specified network address and port. Once a reuqest is received, it is passed to the appropriate handler function for processing.

Anatomy of a ListenAndServe:
```go
http.ListenAndServe(port, ServeMux)
```
Now let's listen on port 8080 and for our ServeMux we are just using the default `http.ServeMux()` by using `nil`. ListenAndServe also returns a err, so we can error check.

```go
// Spinning up the server.
err := http.ListenAndServe(portNum, nil)
if err != nil {
    log.Fatal(err)
}
```

Now run your `main.go` with `go run main.go` and visit `your_ip_here:8080` or `your_ip_here:8080/info` in your browser to see a string printed on your new HTTP server and registered paths. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rhibqg1pt82xema81unm.jpg)
---
Congratulations on starting up a HTTP server, in the next post I'll show you how to add some HTML to this!