/  Technology   /  What is the Easiest Way to Develop a Web Application Using Go?
What is the Easiest Way to Develop a Web Application Using Go?

What is the Easiest Way to Develop a Web Application Using Go?


Now that you are familiar with the basics of Golang and http methods, it is quite natural that you would want to get started with a web application. In this blog, you will get a detailed insight into creating a simple web app from scratch. For this, you won’t have to rely on any Golang web development company. You just have to follow the steps carefully. 


Preliminary Task 

Let’s start by making a directory called webcreate in the go-workspace. To accomplish this, open the Command Prompt on Windows (or Terminal on MAC or Linux) and enter the following code:

cd go-workspace 

mkdir webcreate 

cd webcreate

code .

  • When we type code ., the VS Code opens up. 
  • On Visual Studio Code, we see that the webcreate directory has been created. After that, we make a folder called “format” and a file called “format.html” within it.
  • Inside the webcreate directory, we next create a new folder called static. We also include two other files, marketing.png (which contains a random picture), and format.css.
  • We also include a file called main.go at the end in the directory webcreate. 


Write the Code in the main.go File 

  • Here we have to write the code in the main.go. 

package main

import (
  • As you can clearly see, we have imported four different packages in the program. These are fmt, net/http, html/template, time. 
type Hi struct {
       Sale string
       Time string
  • Next, we create a struct Hi containing two different data fields of type string. These are Sale and Time. After this, we create the function main. 
func main() {
      ola := Hi{"Sale Starts Now", time.Now().Format(time.Stamp)}
      template := template.Must(template.ParseFiles("format/format.html"))

      http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
      http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
             if sale := r.FormValue("sale"); sale != "" {
                      ola.Sale = sale
            if err := template.ExecuteTemplate(w, "format.html", ola); err != nil {
                    http.Error(w, err.Error(), http.StatusInternalServerError)
    fmt.Println(http.ListenAndServe(":8000", nil))


  • As you can see, we have written a lot of code. It is time to understand what exactly we have accomplished here. 
  • We create a variable ola, where we set the data fields of the struct to their initial values. The Sale string contains the phrase “Sale Starts Now.” The time.Now() function displays the time as of right now using Format (time.Stamp).
  • We create another variable named template. In this situation, you need to be aware that the template package includes the Must function. The validity of the template is checked using this during parsing.
  • You may now be asking what a template.Parsefiles() is used for. Well, it is in charge of producing a fresh template. The several template definitions from format/format.html are then parsed.
  • We can use the function http.Handle as we imported the package net/http. The http.Handler is accepted by the handle. To access the static files located in the ‘static’ folder, inside the ‘webcreate’ directory, utilize the http.StripPrefix. It sends the request’s handling to ‘static’ because we specified it as a parameter. 
  • The handler that FileServer offers serves HTTP requests using the file system’s root-level metadata. You are creating a http.Dir type from the string “static”.
  • What accomplishes the procedure HandleFunc? Simply said, it offers a mechanism to draw attention to the proper approach to handle requests for a particular route. For reading and writing purposes, the function (w http.ResponseWriter, r*http.Request) is utilized.
  • The struct data field ola.Sale will now accept the value “sale” if the sale variable reads the FormValue as “sale” and sale is not nil. 
  • Following this, we have to comprehend the role of ExecuteTemplate. In the event of an error, the template application terminates. It is taking the template’s description from format.html into account (which we will check out later in the program). 
  • Since the http package is in charge of providing client and server implementations, it also uses http.StatusInternalServerError to verify the status of any errors.
  • ListenandServe initially calls Serve with Handler while monitoring the TCP network address to handle requests from incoming connections.

I hope you were able to understand what each function was supposed to do. Meanwhile, if you have a difficult project to work on, you can take the help of a Golang web development company


Write the Code in the HTML File 

Now, we will type the code in the HTML file. 

     <meta charset="UTF-8">
     <link rel="stylesheet" href="/static/format.css">

     <title>Welcome {{.Sale}}</title>
   <div class="Hello Centre">Last day {{.Sale}}, {{.Time}}</div>
  • The code stated above is clear to understand. All that we are doing is describing the web page’s outline or structure. The charset property aids in emphasizing the HTML document’s specified character encoding. Additionally, the HTML5 standard requires that developers utilize the UTF-8 character set.
  • The statement <link rel=”stylesheet”….indicates that an external source (format.css) is linked to the current HTML content (format.html). The document’s screen presentation will be determined by the “stylesheet”. 
  • The HTML elements in practice should belong to different classes. To separate the classes, we use <div class= “Hello Centre”> We will be displaying the Last Day along with the Time and value of Sale. 

With this, we finish the HTML document. 


Write the Code in the CSS File 

Following this, we go to the format.css source file which is present inside the ‘static’. We type:

body {
  min-height: 150%;
  background-image: url("/static/marketing.png");
  background-blend-mode:  overlay;
  background-size: cover;

.welcome {
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
  font-size: 2rem;

.center {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center
  • The code that has been written above is easy to figure out. Here, we are just describing the presentation of the web page. In the case of body, we highlight the minimum height, background-image, background-size and background-blend-move. 
  • In the case of .welcome, we specify the font-family, font-size, and color. 
  • For the .center, we specify the height, display, align-items, and justify-content. With this, we wrap up the programming. 


Now when we run the code, and we type on ‘localhost:8000’ on the browser, we get to see:

What is the Easiest Way to Develop a Web Application Using Go?

Thus, the code that has been written is perfectly alright. This is how we create a simple web app. If you have understood the code over here, then you will find it easier to develop intricate web applications later on. 


Leave a comment