i2tutorials

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 .

 

Write the Code in the main.go File 

package main

import (
       "fmt"
       "html/template"
       "net/http"
       "time"
)
type Hi struct {
       Sale string
       Time string
}
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))
}

 

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. 

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

     <title>Welcome {{.Sale}}</title>
 </head>
 <body>
   <div class="Hello Centre">Last day {{.Sale}}, {{.Time}}</div>
 </body>
</html>

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;
  color:lightpink;
}

.center {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center
}

 

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. 

 

Exit mobile version