DEV Community

Steve Omollo
Steve Omollo

Posted on

Building a Simple Web Server in Go

Have you ever wondered how web servers work behind the scenes?

One of the best things about Go is that you can create a working HTTP server with a few lines of code. The standard library already gives us everything we need to start serving web requests without installing any frameworks.

In this tutorial, we will be building a simple web server in Go that:

  • listens on port 8080
  • responds to browser requests
  • serves different routes
  • returns simple text responses

By the end, you will understand the basics of how Go handles HTTP requests and responses.

Prerequisites

To follow along, you should have:

  • Go installed
  • basic familiarity with the terminal
  • beginner-level Go syntax knowledge

You can confirm if Go is installed by running:

go version
Enter fullscreen mode Exit fullscreen mode

Step 1 — Create the Project

Create a new folder for the project:

mkdir simple-go-server
cd simple-go-server
Enter fullscreen mode Exit fullscreen mode

Now initialize a Go module:

go mod init simple-go-server
Enter fullscreen mode Exit fullscreen mode

This creates a go.mod file that helps Go manage dependencies for the project.

Step 2 — Create the Server File

Create a file called main.go.

Your project should now look like this:

simple-go-server/
├── go.mod
└── main.go
Enter fullscreen mode Exit fullscreen mode

Step 3 — Write the HTTP Server

Open main.go and add the following code:

package main

import (
    "fmt"
    "net/http"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "Hello from Go!")
}

func aboutHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w, "This is the about page.")
}

func main() {
    http.HandleFunc("/", homeHandler)
    http.HandleFunc("/about", aboutHandler)

    fmt.Println("Server running on :8080")

    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println("Error starting server:", err)
    }
}
Enter fullscreen mode Exit fullscreen mode

Now let's unpack this.

Understanding the Imports

import (
    "fmt"
    "net/http"
)
Enter fullscreen mode Exit fullscreen mode

We imported two packages:

  • fmt - is used for printing messages
  • net/http - is Go's built-in package for creating HTTP servers

The net/http package is part of Go's standard library, which means we don't need to install anything extra.

Understanding Handlers

This function:

func homeHandler(w http.ResponseWriter, r *http.Request)
Enter fullscreen mode Exit fullscreen mode

is called a handler.

Handlers are functions that respond to incoming HTTP requests.

The parameters mean:

Parameter Purpose
http.ResponseWriter Used to send data back to the client
*http.Request Contains information about the incoming request

Inside the handler, we write a response:

fmt.Fprintln(w, "Hello from Go!")
Enter fullscreen mode Exit fullscreen mode

This sends text back to the browser.

Understanding Routes

Here:

http.HandleFunc("/", homeHandler)
Enter fullscreen mode Exit fullscreen mode

we tell Go:

"When someone visits /, run the homeHandler function."

And here:

http.HandleFunc("/about", aboutHandler)
Enter fullscreen mode Exit fullscreen mode

we register another route.

Now our server has two endpoints:

Route Response
/ Hello from Go!
/about This is the about page.

After registering routes, the final step is starting the server and listening for incoming requests.

Starting the Server

This line starts the web server:

http.ListenAndServe(":8080", nil)
Enter fullscreen mode Exit fullscreen mode

The :8080 means:

"Listen for incoming requests on port 8080."

The nil tells Go to use the default request multiplexer.

Step 4 — Run the Server

Start the application:

go run main.go
Enter fullscreen mode Exit fullscreen mode

You should see:

Server running on :8080
Enter fullscreen mode Exit fullscreen mode

Step 5 — Test the Server

Open your browser and visit:

http://localhost:8080
Enter fullscreen mode Exit fullscreen mode

You should see:

Hello from Go!
Enter fullscreen mode Exit fullscreen mode

Now try:

http://localhost:8080/about
Enter fullscreen mode Exit fullscreen mode

You should see:

This is the about page.
Enter fullscreen mode Exit fullscreen mode

Testing with curl

You can also test the server from the terminal using curl.

For the home route:

curl localhost:8080
Enter fullscreen mode Exit fullscreen mode

For the about route:

curl localhost:8080/about
Enter fullscreen mode Exit fullscreen mode

Here's basically what happens when you visit a route:

  1. Your browser sends an HTTP request
  2. Go receives the request
  3. Go checks which route matches
  4. The correct handler runs
  5. A response is sent back to the browser

This is the foundation of most backend web applications.

Where to Go Next

Our server is very simple, but from here you could add:

  • JSON responses
  • HTML templates
  • middleware
  • databases
  • REST APIs — because eventually, everyone builds one.

This is one reason many developers enjoy Go for backend development — you can start small and gradually build more complex systems while keeping the code readable.

Final Thoughts

Go makes it incredibly easy to get started with backend development.

With a few lines of code, we created a working HTTP server capable of handling multiple routes and serving responses to clients.

If you are learning backend development, understanding the net/http package is a great foundation before moving into larger frameworks and architectures.

Thanks for reading!

Happy coding!

Top comments (0)