Search
Close this search box.

A Guide to Create Your First Web Application with Go

Have you decided to shift your website development requirements to Golang web development? It is the right decision to shift the website app needs to Go if you want a scalable solution using a standard library. The build-up of a relevant web development environment is not that tough and developers can follow the blog to get the relevant information. 

If you aspire for an out-of-the-box web application to fit the business outcomes then Golang is the modern-day development platform for easy use. The installation of the Golang web programming platform should include GOPATH and the file system should be defined correctly in the system. A basic understanding of the Go programming language is vital to use the programs appropriately. 

Golang was developed with the intention to ease the web development activity and it has proved to be the desirable solution for relevant results. The setting up of development platforms for getting web apps with Golang shouldn’t be time-consuming and you’ll be happily surprised to know the right setup of Go. Take the help of experts or relevant tutorials if you want a further detailed version of the web development application activities. 

Prerequisites and Initial Stage of Installation 

To start your Golang web development project, refer to the official site to download the latest version of the Golang programming language. Make sure that the system has the relevant OS installed in it and get the right version of Golang for development activities. The right type of text editor needs to be installed in the system and VSC (Visual Studio Code) is the top option. Developers need to select the compatible text editor that suits the web development in Golang

Follow the right link on the official Golang website and also check the appropriate installation instructions. Define the GOPATH and should be noted correctly. This will be the path to store all the Go projects. 

Once the installation is done correctly, the developer can also change the directory to the GOPATH. Type the relevant command to find the GOPATH on Linux –

env | grey GOPATH 

In case the GOPATH directory is empty then check into the ‘scr’ folder. The creation of a ‘welcome-app’ will be the top option and also create a relevant folder. 

mkdir welcome-app

cd welcome-app 

The directory structure will look like this –

home

 /<your-username>

 /go

 bin/

 //Empty for now 

 src/

 /welcome-app

Building Up a Golang Web Server 

The task of building a basic service should start with sending the text ‘Hello World!’ to the browser and it can be done by making a ‘GET’ request to the server root. The modification ‘main.go’ is represented as  follows –

main.go

package main 

import (

           “net/http”

           “OS”

)

func indexHandler(w http.ResponseWriter, r *http.Request) {

           w.Write([]byte(“<h1>Hello World!</h1>”))

}

func main () {

           port := os.Getenv(“PORT”)

           if port == “” {

           port = “3000”

}

mux := http.NewServerMux()

mux.HndleFunc(“/”, indexHandler)

http.ListenAndServer (“:”+port, mux)

}

In the ‘main’ function, the right attempt is made through the set of variables ‘port’ based on the variable of the ‘PORT’ environment. Also, the use of the HandleFunc method ensures the taking of string patterns for registration of HTTP request handlers. The implementation of ‘Write ()’ method ensures the acceptance of a slice of bytes and writes connections for the HTTP response. 

The compilation and execution of code include –

Command 

$ go build 

Command 

$ ./news-demo-starter-files 

The helloWorld function doesn’t care about the ‘r’ request regardless of the kind of request and always prints ‘Hello World!’. Web server ‘server.go’ is referred to as the second line of the main function and listens to port 8080 as the server requests are made on port 8080. 

Use of Variables on the Go Environment 

The environment variables need to be loaded from a ‘.env’ file to a new environment in Golang web programming. The environment variables need to be defined in a ‘.env’ file and load them correctly at the initial stages of the program. Create a ‘.env’ file in the root of the project directory and then open it in the text editor –

bash 

$ vim .env

Within the development file, setup the ‘PORT’ environment: 

.env

PORT=3000

Also, run the command in the project directory and install the ‘godotenv’ package –

bash 

$ go get github.com/joho/godotenv

Now, the ‘main.go’ file needs to be updated as –

main.go

package main 

import (

“log”

“net/http”

“OS”

“github.com/joho/godotenv”

)

func indexHandler(w http.ResponseWriter, r *http.Request) {

           w.Write([]byte(“<h1>Hello World!</h1>”))

}

Func main () {

           err := godotenv.Load()

           if err != nil {

                       log.Println(“Error loading .env file”)

}

port := os.Getenv(“PORT”)

if port == “” {

           port = “3000”

}

mux := http.NewServeMux()

mux.HandleFunc(“/”, indexHandler)

http.ListenAndServe(“:”+port, mux)

}

Appropriate Use of Golang’s Template Package 

Let us now understand Golang templating and if developers are aware of templating in other languages, then it becomes easy to understand it in Go. The templates for the web app with Golang ease out the output customization of web apps based on the specific route without writing codes in similar places. 

The Golang language provides two template packages like ‘text/template’ and ‘html/template’ in the Go standard library. These are both similar interfaces and the ‘html/template’ package is the right way to generate reliable output that ensures code injection safety. 

Import ‘html/template’ in the ‘main.go’ file in the following way –

main.go 

package main 

import (

           “html/template”

           “log”

           “os”

           “github.com/joho/godotenv”

)

Var tpl = template.Must(template.ParseFiles(“index.html”))

Func indexHandler(w http.ResponseWriter, r *http.Request) {

}

The ‘tpl’ template execution in the ‘indexHandler’ function will provide two arguments – one where the developer wants to write the output and the data to be passed across the template. 

Final Thoughts! 

The web development in Golang can be set up easily using the above-mentioned steps and start the tasks on an early basis. We have covered different steps to set up the Go platform and also starting the web development tasks for functional apps. Take the help of professional web developers with relevant experience in building a supportive environment for web app development. The setting up of a simple web server and running of relevant commands will ensure quick startup with the app development tasks.

Facebook
Twitter
LinkedIn
WhatsApp

Related Posts

Get Curated Post Updates!

Sign up for my newsletter to see new photos, tips, and blog posts.

Subscribe to My Newsletter

Subscribe to my weekly newsletter. I don’t send any spam email ever!