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
Step 1 — Create the Project
Create a new folder for the project:
mkdir simple-go-server
cd simple-go-server
Now initialize a Go module:
go mod init simple-go-server
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
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)
}
}
Now let's unpack this.
Understanding the Imports
import (
"fmt"
"net/http"
)
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)
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!")
This sends text back to the browser.
Understanding Routes
Here:
http.HandleFunc("/", homeHandler)
we tell Go:
"When someone visits
/, run thehomeHandlerfunction."
And here:
http.HandleFunc("/about", aboutHandler)
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)
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
You should see:
Server running on :8080
Step 5 — Test the Server
Open your browser and visit:
http://localhost:8080
You should see:
Hello from Go!
Now try:
http://localhost:8080/about
You should see:
This is the about page.
Testing with curl
You can also test the server from the terminal using curl.
For the home route:
curl localhost:8080
For the about route:
curl localhost:8080/about
Here's basically what happens when you visit a route:
- Your browser sends an HTTP request
- Go receives the request
- Go checks which route matches
- The correct handler runs
- 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)