Open In App

How to Create Modules in Golang?

Last Updated : 28 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Go has included support for versioned modules as proposed here since 1.11 with the initial prototype as vgo. The concept of modules in Go was introduced to handle the problem of dependencies within your Go applications. Several packages are collected and combined to form a module which is stored in a tree-like structure in a file with a go.mod file at its root.

Note: Update current version of Go in your system to the latest one to work with Go modules.

Let us now see how to create modules in Golang. To create a module first create a directory and then go inside it using the following commands:

mkdir go_modules
cd go_modules

To initialize the current directory as the root of the module that will allow us to manage dependencies, use the following command:

go mod init go_modules

As we are working outside the $GOPATH/src module, we need to explicitly specify the name of the module during initialization.

Go-Modules-1

We can now check if the go.mod file is created and if present, the contents of it.

Next step is to create a simple Go file with the following code:




// file inside the current module
package gfg_go
  
import("fmt")
  
func initialiser() string {
  
       fmt.Printf("In gfg_go package. \n")
  
       // returns the current module
       // and the package name
       return_string := "Module : go_modules."
       return return_string
  
}


For testing the above function, we create another Go file with the following code:




package gfg_go
  
import (
       "testing"
       "fmt"
       "strings"
)
  
// function to test if the original
// go program is working
func TestFunction(test *testing.T){
  
       test_string1 := "go_modules"
  
       // calling the function from
       // the previous go file
       res := strings.Split(initialiser(), ":")
  
       // removing spaces and line-ending
       // punctuation before comparing
       test_string2 := strings.Trim(res[1], " .")
  
       if test_string1 == test_string2 {
               fmt.Printf("Successful!\n")
  
       } else {
                // this prints error message if
                // strings do not match
               test.Errorf("Error!\n")
       }
  
}


After running the go test command, we should see that our test builds passed!

Adding dependencies to Go Module

To add dependencies to our module, we will use the require() function along with the version of the module being used:




module go_modules
  
go 1.14
  
require github.com/rs/zerolog v1.14.3


All the dependencies in our package are shown as follows:

 Go-Modules-4

And all the files in our directory are listed below:

 Go-Modules-5

Note: The go command uses the go.sum file to ensure that future downloads of these modules retrieve the same bits as the first download and the modules your project depends on do not change unexpectedly.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads