How to Create Modules in Golang?

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:

filter_none

edit
close

play_arrow

link
brightness_4
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
  
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
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")
       }
  
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.