Go Make Your First Http Request

Created At: 2023-05-17 02:29:48 Updated At: 2023-05-17 04:06:22

Here we will take a quick look how to make your first http request in Go language. Since this ia very trendy language, it's worth learning how to make a server and keep it up and running. 


Make a project

First let's go ahead a make project and so we will create a folder and name it FirstProject. Go inside this terminal and run 

go mod init restfulapi

Then you will see the below changes in your project

Since you ran the command you see a go.mod file inside the project.

Then we will create a rest.go file(you may call it main.go, this is the convention though) inside the FirstProject. 

Inside rest.go, we will place the below code, 

package main

import "fmt"

func main() {
	fmt.Println("Hello World")
}

If you run, the below command

go build . && go run .

Then you will see "Hello World" on the console.

We will seperate our Http logic from main file which is rest.go. That's why we create a new folder and cd to it.

mkdir Handler && cd Handler

With the above command we will have Handler file and we will be inside that.

Inside that file we will create a new file name httpHandler.go and put the code below

package httplayer

import (
	"net/http"

	"github.com/gorilla/mux"
)

type MyHandler struct {
	Router *mux.Router
	Server *http.Server
}

func ServerProvider() *MyHandler {
	mH := &MyHandler{}

	return mH
}

Package name

We have created a package name httplayer. You may name it anything.

Imports

Since we will be dealing with routing and server so we have imported net/http and github.com/gorilla/mux. They are very important in any kind of Go lang project.

Struct

Since we have a better programming skills, we wanna wrap all the objects inside a Struct. That's why we created two fields inside struct. Our struct is MyHandler

Server

ServerProvider() function is the actual one that provides raw materials for our server. This just creates a Handler object and returns it. We need to return a ServerProvider object since we want to use the object to create a server. 

Remember ServerProvider() just provides the raw material for our app and then it creates the server.

The below code does the magic 

mH.Server = &http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: mH.Router,
	}

Routes

Now, it's time to define our end points. End points are defined using mux.NewRouter() object.  First we will create an object of mux.NewRouter() and we will use the newly created object to call function. That called function would contain our  end points.

func NewServer() *MyHandler {
	mH := &MyHandler{}
	mH.Router = mux.NewRouter()
	mH.mapRoutes()
        mH.Server = &http.Server{
		Addr:    "127.0.0.1:8080",
		Handler: mH.Router,
	}
	return mH
}

func (mH *MyHandler) mapRoutes() {
	mH.Router.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "This reponse is from the server. I in love with Go lang")
	})
}

You should notice that, we have updated our NewServer() function, And we created an object of mux.NewRouter() and on that object we called mapRoutes() function. This function defined our end points.

Our end point is "/hello"

We need one more thing to do. Now we have every thing to make the server up and running. 

func (mH *MyHandler) MyServer() error {
	if err := mH.Server.ListenAndServe(); err != nil {
		return err
	}
	return nil
}

With the above code, the server is up and running.

Now we need to call it from our rest.go file. Since it's a receiver function of MyHandler struct, we would be to call it from MyHandler struct object.

That's why we need to change our rest.go file and in it, we will have to import the httplayer package

package main

import (
	"fmt"
	httplayer "restfulapi/Handler"
)

func main() {

	fmt.Println("Hello World")

	httpObject := httplayer.ServerProvider()
	httpObject.MyServer()
}

You may see Hello World, which tells the server is running smothly. 

Now verify with Postman

We got the response. 

Comment

Add Reviews