The Wayback Machine - https://web.archive.org/web/20240118180411/https://www.geeksforgeeks.org/how-to-build-a-simple-web-server-with-golang/
Open In App
Related Articles

How to Build a Simple Web Server with Golang?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

Golang is a procedural programming language ideal to build simple, reliable, and efficient software. 

Creating Web Servers Using Golang: 

Initialize a project

Create a project folder with a .go file inside eg. server.go.
 

Directory structure:

The server.go file:

Go




package main
 
import (
    "fmt"
    "log"
    "net/http"
)
 
func main() {
 
    // API routes
    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello world from GfG")
    })
    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hi")
    })
 
    port := ":5000"
    fmt.Println("Server is running on port" + port)
 
    // Start server on port specified above
    log.Fatal(http.ListenAndServe(port, nil))
}


Run the server using the following command (make sure you’re in the project directory):

go run server.go

Note:  You have to stop the server with Ctrl + C and restart via the same command whenever the server.go file is changed. 
 

Console:

Open the desired web browser and any of these URLs to verify if the server is running:

http://localhost:5000/ or http://localhost:5000/hi

Output:

Serving static files:

Create a static folder with all the static files.
Example directory structure:

Example static files:

HTML




<html>
  <head>
    <title>Home</title>
  </head>
  <body>
    <h2>Home page</h2>
  </body>
</html>


 
 

  • The about.html file

 

HTML




<html>
  <head>
    <title>About</title>
  </head>
  <body>
    <h2>about page!</h2>
  </body>
</html>


Now Edit the server.go file:

Go




package main
 
import (
    "fmt"
    "log"
    "net/http"
)
 
func main() {
 
    // API routes
 
    // Serve files from static folder
    http.Handle("/", http.FileServer(http.Dir("./static")))
 
    // Serve api /hi
    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hi")
    })
 
    port := ":5000"
    fmt.Println("Server is running on port" + port)
 
    // Start server on port specified above
    log.Fatal(http.ListenAndServe(port, nil))
 
}


Verifying if static files are served:


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 10 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials