DEV Community

Cover image for Building a simple web server with Go👨‍💻
Ekemini Samuel
Ekemini Samuel

Posted on

Building a simple web server with Go👨‍💻

Daily Go👨‍💻

It is absolutely important to write and build simple basic programs when learning programming.
I am working with Go and we will be building a simple web server.

Let's begin

First, a web server is a program that listens for incoming requests from web clients (such as browsers) and sends back the appropriate response.

Be sure to have go installed to run this program.
Here's a link to install go.

Building a simple web server with Go is a relatively straightforward task that can be accomplished in just a few lines of code. In this post, we will walk through the process of building a simple web server using the Go programming language.

Step 1: Import the necessary packages

The first step in building a web server with Go is to import the necessary packages. We will be using the "net/http" package, which provides the necessary functions for building a web server in Go. Here is the code for importing the necessary packages:



import (
    "fmt"
    "net/http"
)



Enter fullscreen mode Exit fullscreen mode

Step 2: Define the handler function

The next step is to define the handler function, which will be responsible for handling incoming requests. This function will take an http.ResponseWriter and an http.Request as its arguments. The http.ResponseWriter is used to write the response to the client, and the http.Request contains information about the incoming request.

Here is an example of a simple handler function that will write "Hello, World!" to the response:



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



Enter fullscreen mode Exit fullscreen mode

Step 3: Register the handler function

Now that we have defined our handler function, we need to register it with the http package so that it can be called when a request is received. We do this using the http.HandleFunc() function.

Here is an example of how to register the handler function:



http.HandleFunc("/", handler)


Enter fullscreen mode Exit fullscreen mode

Step 4: Start the server

Finally, we are ready to start our web server. We do this using the http.ListenAndServe() function. This function takes two arguments: the first is the address to bind to, and the second is the handler to use.

Here is an example of how to start the server:



http.ListenAndServe(":8080", nil)



Enter fullscreen mode Exit fullscreen mode

Note that in the above example, we are binding to port 8080. You can change it to any other port you like.

The complete code for our simple web server looks like this:



package main

import (
    "fmt"
    "net/http"
)

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

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



Enter fullscreen mode Exit fullscreen mode

To run the above code you can use the following command:



go run main.go



Enter fullscreen mode Exit fullscreen mode

This is a very basic example of how to build a web server with Go, but it should give you a good starting point for building more complex applications. With a little more work, you can add routing, template rendering, and other features to your web server.

Till next time.
⚡codeDaily

Top comments (0)