Creating a Simple Web Server in Go using the net/http Package

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setting Up Go
  4. Creating a Simple Web Server
  5. Testing the Web Server
  6. Conclusion

Introduction

Welcome to this tutorial on creating a simple web server in Go using the net/http package. By the end of this tutorial, you will learn how to set up Go, create a basic Go web server, and test it.

Prerequisites

To follow along with this tutorial, you should have some basic knowledge of programming concepts. Familiarity with Go syntax and basics will be helpful but not mandatory.

Setting Up Go

Before we begin, make sure you have Go installed on your machine. You can download and install Go from the official Go website at https://golang.org/dl/.

Once Go is installed, verify the installation by opening a terminal or command prompt and running the following command:

go version

This command should display the installed Go version, confirming that Go is set up correctly.

Creating a Simple Web Server

Now that we have Go set up, let’s create a simple web server. Open a text editor and create a new file called server.go. We will write our Go code in this file.

package main

import (
	"fmt"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Welcome to my Go web server!")
}

func main() {
	http.HandleFunc("/", handler)
	http.ListenAndServe(":8080", nil)
}

In the above code, we import the necessary packages: fmt for basic input/output operations and net/http for creating an HTTP server.

The handler function is our request handler. It takes two parameters, w (http.ResponseWriter) and r (http.Request). Inside the function, we use fmt.Fprintln to write “Welcome to my Go web server!” as the response.

In the main function, we use http.HandleFunc to associate our handler function with the root URL (“/”). Finally, we use http.ListenAndServe to start the web server on port 8080.

Testing the Web Server

To test our web server, open a terminal or command prompt and navigate to the directory containing server.go. Run the following command:

go run server.go

This will compile and run our Go program. If everything is set up correctly, you should see the following output:

Listening on :8080...

Open a web browser and visit http://localhost:8080/. You should see the message “Welcome to my Go web server!” displayed in the browser.

Congratulations! You have created a simple web server in Go.

Conclusion

In this tutorial, we learned how to create a simple web server in Go using the net/http package. We covered the basics of setting up Go, writing a request handler function, and starting the server. You can now build upon this knowledge to create more sophisticated web applications using Go.

Feel free to explore more features of the net/http package, such as routing, handling different HTTP methods, or serving static files. Go’s standard library offers powerful tools for web development.

Remember to continually practice and experiment with Go to strengthen your skills. Happy coding!