Go Language

Writing a Basic Web Application in Go


Introduction

Go (Golang) is a versatile language that can be used for web development. In this guide, we'll walk you through the process of writing a basic web application in Go, including creating routes, handling requests, and rendering HTML templates. You'll learn the key concepts and be provided with sample code to get started.

Creating a Web Server

To create a web application in Go, you start by creating a web server. This server listens on a port and handles incoming HTTP requests. Here's an example of creating a simple web server:

                            package main
                import (
                    `fmt`
                    `net/http`
                )
                func handler(w http.ResponseWriter, r *http.Request) {
                    fmt.Fprintln(w, `Hello, Go Web Application!`)
                }
                func main() {
                    http.HandleFunc(`/`, handler)
                    http.ListenAndServe(`:8080`, nil)
                }
                    

In this code, we create an HTTP server that listens on port 8080 and responds with `Hello, Go Web Application!` to all incoming requests.

Routing and Handling Requests

In a web application, routing is essential for directing requests to the appropriate handlers. You can define routes and handle different HTTP methods. Here's an example of handling a specific route:

                            package main
                import (
                    `fmt`
                    `net/http`
                )
                func helloHandler(w http.ResponseWriter, r *http.Request) {
                    fmt.Fprintln(w, `Hello, Web Application!`)
                }
                func main() {
                    http.HandleFunc(`/hello`, helloHandler)
                    http.ListenAndServe(`:8080`, nil)
                }
                    

In this code, we define a route at `/hello` and respond with `Hello, Web Application!` when that route is accessed.

Rendering HTML Templates

A basic web application often involves rendering HTML templates to display dynamic content. You can use the `html/template` package to achieve this. Here's an example of rendering a simple HTML template:

                            package main
                import (
                    `fmt`
                    `html/template`
                    `net/http`
                    `os`
                )
                func helloHandler(w http.ResponseWriter, r *http.Request) {
                    t, _ := template.New(`webpage`).Parse(`<html><body><h1>Hello, {{.}}!</h1></body></html>`)
                    t.Execute(w, `Web Application`)
                }
                func main() {
                    http.HandleFunc(`/hello`, helloHandler)
                    http.ListenAndServe(`:8080`, nil)
                }
                    

In this code, we create an HTML template and use it to render a dynamic `Hello, Web Application!` message in the response.

Further Resources

To continue learning about writing web applications in Go, consider these resources:

Written by Surfside Media

Senior Full Stack Developer specializing in Web Technologies.