Build Web Application With Golang

build web application with golang

In this article, you will understand how to build a Web application using a Golang web server that you have at your disposal.

So, let’s start with an empty script and just want to import the package.

The package main and then we’re going to import built-in Golang library have formatted and you just parentheses format.

package main

import ("fmt","net/http")

all these as part of your standard library so you should have both of those. now, we’re going to go ahead and do is we’re gonna create our main function.

package main

import ("fmt","net/http")

func main()
{
  http.HandleFunc("/",index_handler)
} 

So, func main and then in our main function beginning. we’re gonna creating handlers so basically in Golang web development you’re gonna have basically you know something has to take that URL that someone enters in and it’s gonna have some sort of path to it and then you have to take that path and then figure out what kind of function corresponds to that path so the thing that’s going to do that is going to be the handler so we’re going to use Http.HandleFunc() and that’s going to take in the path.

http.HandleFunc("/",index_handler)

We’re just gonna use a forward slash which just a forward slash would be the index right the homepage right there is nothing there and then we’re gonna have whatever we’re gonna whatever function we want to run in this case it’s going to be index Handler that we want to run.

Now, it’s beyond actually handling functions we need to have we need our server. so we’re an HTTP listen and serve

The first parameter here is going to be on what port and then the second parameter is like server stuff.

http.ListenAndServe(":8080",nil)

So, this creates our server at basically localhost port

Now, we need an index handler you have to make that function. then go ahead and do is make that function.

Let’s declare func index_handler and then index handler is gonna have basically two parameters that are gonna come into it.

package main

import ("fmt" "net/http")

func index_handler(w http.ResponseWriter,r *http.Request)
{
 fmt.Fprint(w, "Welcome to golang")
}

func about_handler(w http.ResponseWriter,r *http.Request)
{
 fmt.Fprint(w, "About Page")
}

func main()
{
  http.HandleFunc("/",index_handler)
  http.HandleFunc("/about",index_handler)
  http.ListenAndServe(":8080",nil)
} 

first, it’s gonna have W which is gonna be your writer. the type is going to be an HTTP ResponseWriter.

The writer and that’s what’s gonna be input information onto the page and then you also need the HTTP requests. we’re going to use format Fprint. this format print whatever you specified it’s gonna format based on what you specify which we’re gonna say is the writing itself and then and then it’s gonna it’s got output whatever.

Now let’s go ahead and run following golang command.

go run gotut.go

You can head over to a browser head to localhost on the port.

go programming output
go programming routing

Leave a Reply

Your email address will not be published. Required fields are marked *