How to Build a Web Application with Golang?
Tags: go, webdev, react, programming
Published: 2020-01-13 08:23:37

**Go** is a compiled programming language that combines efficiency and security and other *static languages*. It is a language of youngsters, and everyone wants to build an application with Golang as it improves the modern, multicore computers and other social networking demands. Golang is a great programming language that can easily resolve problems such as the native currency model, no packaging tool, and much more. **Golang web development** is emerging as one of the most advanced and universally used programming languages worldwide.

#Importance of Golang Web Development

[Golang](https://golang.org/) is an advanced programming language that gradually makes programming language much more accessible. Go is developed by Google that improves designing processes due to its extensive standard library. These currencies distribute codes and help them to learn user-friendly syntax.
**Main Golang Guide: [Top Golang Frameworks and Tools](https://www.brsoftech.com/blog/golang-development-guide-top-golang-frameworks-and-tools/)**

###Step By Step Process of Web Application Development

The one who needs to build a web application development needs to focus on the webserver and other nodes that need to be handled. The web server needs to use a good HTML template that helps in loading parameters and directory for better flexibility. Let’s start with the process of building an application with [Golang Development Company](https://www.brsoftech.com/golang-development-company.html).

![Alt Text](https://www.brsoftech.com/blog/wp-content/uploads/2019/09/Advantages-and-Disadvantages-of-Golang-1.jpg)
  **[The Advantages and Disadvantages of Golang for Creating Web Application](https://www.brsoftech.com/blog/advantages-and-disadvantages-of-golang-web-application/)**

**Important Requirements**
Before developing a web application, firstly, you have to install the Golang programming language in your device. It is important to download the latest version of Go, who is very much familiar with syntax and constructs. 

**Create a Web Server**
After examining all the requirements, we need to create a simple server that can run on the server go. Once we can try it on run server, we need to browse, and straightaway proceed to (http://localhost:8081/world). This page shows you some queries and returned back to you in the hello world.

### package main
        import (
    "fmt"
    "html"
    "log"
    "net/http")
func main() 

    {http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path))
    })

    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request){
        fmt.Fprintf(w, "Hi")
    })

    log.Fatal(http.ListenAndServe(":8081", nil))}

**Add Some Complexity**
After setting up the server, we need to move and try to upgrade our counter in every specific time as these special URL, and web server needs to be a safeguard using the counter or adding some complexities like mutex in order to prevent the server from being hit with bugs and malfunctioning.

### package main
        import (
    "fmt"
    "log"
    "net/http"
    "strconv"
    "sync")
var counter int
var mutex = &sync.Mutex{}
func echoString(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "hello")}
func incrementCounter(w http.ResponseWriter, r *http.Request) {
    mutex.Lock()
    counter++
    fmt.Fprintf(w, strconv.Itoa(counter))
    mutex.Unlock()}
func main() {
    http.HandleFunc("/", echoString)

    http.HandleFunc("/increment", incrementCounter)

    http.HandleFunc("/hi", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hi")
    })

    log.Fatal(http.ListenAndServe(":8081", nil))}


**Serving Static Files **

Now we can move on to static files by creating static folders that create simple HTML files keeping asynchronous nature of the Golang web server.

<.html>
<.head>
 <.title>Hello World</title>
  <./head>
  <.body>
    <.h2>Hello World!</h2>
  <./body>
<./html>
**Note:-**(sorry for (.)Actually if you want to use this code please remove (DOT)

Once you alter the changes using the web code, you need to make a request that contains index.html that helps in the surfing browser.

**Serving Content from the Browser**

Create a directory with static project files that contain all the required data in our static files.


package main

import (
    "log"
    "net/http")

func main() 
{http.Handle("/", http.FileServer(http.Dir("./static")))

    log.Fatal(http.ListenAndServe(":8081", nil))}

**Serving Content on HTTP**
Create a web server by serving content on Http.
package main

import (
    "log"
    "net/http")

func main() 

  {http.Handle("/", http.FileServer(http.Dir("./static")))

    log.Fatal(http.ListenAndServeTLS(":443", "server.crt", "server.key", nil))}

**Generate Keys**

Now, at last, you need to generate keys and start your Http by running on main.go. 

**$ openssl genrsa -out server.key 2048
$ openssl ecparam -genkey -name secp384r1 -out server.key
$ openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
[Source](https://tutorialedge.net/golang/creating-simple-web-server-with-golang/)** 

![Alt Text](https://www.brsoftech.com/blog/wp-content/uploads/2019/12/Go-Web-Framework.jpg)
**>>>[Read More: Top Google Go Web Framework](https://www.brsoftech.com/blog/best-golang-web-framework/)<<<**

###Conclusion

Go is an easy to read language that can improve errors and bugs more frequently. It is a more advanced and robust programming language. It is specially designed for next-generation enterprises for fast general-purpose language with no try-catch blocks and no fancy syntax used. Those who want to build a web application can take a reference from this post.
![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/q4p7uljjadd1vh6a4rad.jpg)