惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Recent Announcements
Recent Announcements
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
博客园 - 【当耐特】
量子位
有赞技术团队
有赞技术团队
博客园 - 三生石上(FineUI控件)
博客园 - Franky
M
MIT News - Artificial intelligence
U
Unit 42
Last Week in AI
Last Week in AI
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
J
Java Code Geeks
V
Visual Studio Blog
Engineering at Meta
Engineering at Meta
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
MyScale Blog
MyScale Blog
T
Tailwind CSS Blog
T
The Blog of Author Tim Ferriss
V
V2EX

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Building a Simple Web Server in Go
Steve Omollo · 2026-05-13 · via DEV Community

Steve Omollo

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!