The Hypertext Transfer Protocol (HTTP) is the foundation of data communication on the web. A web server listens for HTTP requests from clients (usually web browsers) and sends back HTTP responses. An HTTP request consists of a method (e.g., GET, POST), a URL, headers, and an optional body. An HTTP response contains a status code, headers, and a body.
net/http
PackageGo has a built - in net/http
package that provides a simple and powerful way to build web servers. It includes functions for handling requests, routing, and serving files.
Create a new directory for your project. Open your terminal and run the following commands:
mkdir mywebserver
cd mywebserver
Inside the mywebserver
directory, create a new Go file named main.go
.
Here is a simple example of a basic web server in Go:
package main
import (
"fmt"
"net/http"
)
// handler function
func helloHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World!")
}
func main() {
http.HandleFunc("/", helloHandler)
fmt.Println("Starting server on port 8080...")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
In this code:
helloHandler
function takes two parameters: http.ResponseWriter
and *http.Request
. The http.ResponseWriter
is used to send the response back to the client, and the *http.Request
contains information about the incoming request.http.HandleFunc
maps the root path (/
) to the helloHandler
function.http.ListenAndServe
starts the server on port 8080.To run the server, open your terminal in the mywebserver
directory and run:
go run main.go
Now, open your web browser and navigate to http://localhost:8080
. You should see the message “Hello, World!”.
You can handle multiple routes in your web server. Here is an example:
package main
import (
"fmt"
"net/http"
)
func homeHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the home page!")
}
func aboutHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "This is the about page.")
}
func main() {
http.HandleFunc("/", homeHandler)
http.HandleFunc("/about", aboutHandler)
fmt.Println("Starting server on port 8080...")
err := http.ListenAndServe(":8080", nil)
if err != nil {
fmt.Println("Error starting server:", err)
}
}
In this code, the root path (/
) is mapped to the homeHandler
, and the /about
path is mapped to the aboutHandler
.
To serve static files like HTML, CSS, and JavaScript, you can use the http.FileServer
function. First, create a static
directory in your project and add an index.html
file inside it.
<!-- static/index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<title>Static Page</title>
</head>
<body>
<h1>This is a static page.</h1>
</body>
</html>
Here is the Go code to serve the static files:
package main
import (
"net/http"
)
func main() {
http.Handle("/", http.FileServer(http.Dir("./static")))
http.ListenAndServe(":8080", nil)
}
Now, when you navigate to http://localhost:8080
in your browser, you will see the content of the index.html
file.
Always handle errors properly. For example, when starting the server, check the error returned by http.ListenAndServe
as shown in the previous examples.
Use a more advanced router for complex applications. Popular routers in the Go ecosystem include gorilla/mux
and httprouter
.
Implement logging to keep track of requests and errors. The log
package in Go can be used for basic logging.
package main
import (
"fmt"
"log"
"net/http"
)
func handler(w http.ResponseWriter, r *http.Request) {
log.Printf("Received request: %s %s", r.Method, r.URL.Path)
fmt.Fprintf(w, "Hello!")
}
func main() {
http.HandleFunc("/", handler)
log.Println("Starting server on port 8080...")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("Error starting server:", err)
}
}
In this tutorial, we have learned how to build a basic web server in Golang. We covered fundamental concepts such as the HTTP protocol and the net/http
package, created a simple web server, handled different routes, served static files, and discussed common and best practices. With this knowledge, you can start building more complex web applications using Go.