How to Use Module in Golang

golang
Reading Time: 3 minutes

Hi folks!! In this blog, we are gonna learn How to Use a Module in Golang. We will see how to create a go module and how to add a dependency.

What is Go Module?

Go module is a collection of Go packages stored in a file tree with a go. mod file at its root. It is a dependency management system for go that makes dependency version information explicit and easier to manage. The module allows having a workspace outside GOPATH. All the modules which are needed or to be used in the project are managed in go. mod file.

Creating a Go Module

Creating a module in go is quite easy just run the command go mod init <module_name> this module name should be unique. Make sure this package name is accessible if you want to git clone it. for example, you write a library and other people want to use your code so they can use go get commands to easily grab your module. If you are not sure that you will upload your module eg to GitHub or somewhere else you can change your module in go. mod file.

go mod init github.com/ahmad33/my-modules

When you will run the above command it will create a new module, initializing the go. mod the file that has the definition of the module. This file gives the idea of some third party or somebody who will be running your code that this entire code is dependent on which version of Golang.

Adding a dependency

So will use a routing system which is Gorilla Mux. To use this routing system first we have to install this by using the go toolchain to work with it.

The go toolchain is the way how you pull all the dependencies. So run the following command to install.

go get -u gihtub.com/gorilla/mux

Now run the above command in the same directory where you have to initialize go. mod file. When you will run the above command it will go on to the web and will bring all the assets from the web itself.

So GitHub directory will be directly fetched.

module github.com/ahmad33/my-modules

go 1.13

require github.com/gorilla/mux v1.8.0 // indirect

The file indicates that it is using version 1.13 and there is require and it says github.com/gorilla/mux so this is the library that comes in with version 1.8.0.

Here indirect indicates that as of now your code is in the main. go or any other file is not using this. As soon as you will start using it, it will go away.

Now it also creates a go. sum file that maintains the checksum. So when you run the project again it will not install all packages again. But use the cache which is stored inside $GOPATH/pkg/mod directory (module cache directory).

go env

Now we will use the downloaded library in our code. so create a file main. go and paste the below code:

package main
import (
   "fmt"
   "net/http"
   "github.com/gorilla/mux"
   "log"
  
)

func main()  {
   fmt.Println("Hello mod in golang")
   greeter()
   r := mux.NewRouter()
   r.HandleFunc("/", serveHome).Methods("GET")

   log.Fatal(http.ListenAndServe(":4000",r))
}
func greeter(){
   fmt.Println("hi mod users")
}
func serveHome(w http.ResponseWriter, r *http.Request)  {
      w.Write([]byte("<h1>Welcome to golang series on knoldus Blog</h1>"))   
}

First Compile the above code using the below command:

go build .

After that run the code using the below command:

go run main.go

Now to verify that our code is successfully deployed open the browser and use <IP>:4000 and you will see the output.

References:

https://go.dev/blog/using-go-modules

knoldus

Written by 

Jubair Ahmad is a Software Consultant (DevOps)at Knoldus.Inc.He loves learning new technology and also have interest in playing cricket.