DEV Community

Cover image for How to Build a Web Application with Golang?
Jitendra Dadhich
Jitendra Dadhich

Posted on

How to Build a Web Application with Golang?

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 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

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.

Alt Text
The Advantages and Disadvantages of Golang for Creating 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")
Enter fullscreen mode Exit fullscreen mode

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))}
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode

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))}
Enter fullscreen mode Exit fullscreen mode

*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
<./head>
<.body>
<.h2>Hello World!
<./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))}
Enter fullscreen mode Exit fullscreen mode

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))}
Enter fullscreen mode Exit fullscreen mode

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

Alt Text
>>>Read More: Top Google Go 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

Top comments (1)

Collapse
 
devchau63740719 profile image
Dev Chaudhary • Edited

Hey @jitendradadhich , You have described a trending topic, people are actually want to know about this type of topic, So thanks for sharing here,