Sling Academy
Home/Golang/Building an HTTP Server in Go in 2 Minutes

Building an HTTP Server in Go in 2 Minutes

Last updated: November 27, 2024

Go, also known as Golang, makes it incredibly easy to set up a simple HTTP server with just a few lines of code. In this article, we will walk through building a basic HTTP server in under 2 minutes.

Setup Your Go Environment

Ensure you have Go installed on your machine. You can download it from the official Go downloads page. Once installed, verify by running the following command:

go version

Create Your Go Server File

Start by creating a new directory where your project files will reside. Inside this directory, create a file named main.go:

mkdir go-http-server
cd go-http-server
touch main.go

Write Your Go HTTP Server Code

Edit the main.go file and add the following code:

package main

import (
    "fmt"
    "net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Hello, World!")
}

func main() {
    http.HandleFunc("/", helloHandler)
    fmt.Println("Starting server at port 8080...")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}

Run Your HTTP Server

To start the server, open your terminal, navigate to the project directory, and run:

go run main.go

Your server is now running on localhost at port 8080. You can visit http://localhost:8080 in your web browser to see the message "Hello, World!" displayed.

Understanding the Code

The Go code above does the following:

  • import "net/http": This brings in Go's net/http package which provides HTTP client and server implementations.
  • helloHandler: This function handles HTTP requests. When invoked, it writes a simple "Hello, World!" response.
  • http.HandleFunc("/", helloHandler): This sets up a route at / to use the helloHandler function whenever a request is made to this path.
  • http.ListenAndServe(":8080", nil): This starts the server on port 8080.

That's all you need to create a basic HTTP server in Go. You can now extend this server by adding more routes and handlers as needed for more complex applications.

Next Article: Handling JSON Requests and Responses in Go

Series: Networking and Server

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant