In this post, we will learn how to create our first Go module and how to call Golang function from another module or directory. This will form the basis for our future posts on Golang.

However, before starting this post I would recommend you to go through the Golang Hello World post.

1 – What is the Need of Modules?

Modules are the building blocks for complex applications. We can collect one or more packages within a module to create a set of useful functionalities.

In Golang, we group code into packages. One or more packages forms a module. Every module specifies the dependencies it needs to run. This includes the Go version and also the details about any external dependencies.

You can check out this post about importing external dependencies in a Go program.

Once we create and publish a module, we can also add more functionalities to the same module. Basically, this creates a new version. Developers using our module can also choose to upgrade to the new version or stay with the old version.

2 – Initializing First Go Module

We will create a module that helps us generate a dynamic greeting message.

Let us start by creating a new directory to write our code.

$ mkdir greeting-module
$ cd greeting-module

Next, we initialize our module by executing the init command.

$ go mod init progressivecoder.com/greeting
go: creating new go.mod: module progressivecoder.com/greeting

Basically, we provide a path to our module. In this case, the path is progressivecoder.com/greeting. If we publish our module, this will be the path to download our module. More on publishing modules in a later post.

But what does the go mod init command really do?

This command basically creates a go.mod file in the greeting-module directory. This file tracks our module’s dependencies. When first created, the file only contains the name of our module and the Go version. See below:

module progressivecoder.com/greeting

go 1.17

As we add dependencies to our module, they also get tracked within this file. This helps create reproducible builds for our Go module.

3 – Writing the Golang Function

Next, we simply write the actual program in the greeting.go file.

package greeting

import "fmt"

func HelloGreeting(name string) string {
	message := fmt.Sprintf("Hello %v", name)
	return message
}

Let us understand the above code:

  • We first declare a greeting package. Packages are like a collection of functions.
  • Next, we implement the HelloGreeting function. This function basically takes name as input. We have to specify the type of data name parameter will contain. In this case, it is string.
  • We also specify the output data type. That is also a string.
  • In Go, a function whose name starts with capital letter (such as HelloGreeting) can be called by a function from another package. In other words, the functions starting with capital letter names are exported functions.
  • Next, we declare a message variable to hold the greeting. In Go, the := operator is a shortcut to declare and initialize a variable.
  • We use the Sprintf function from the fmt package for creating a greeting message. The first argument is a format string. Sprintf substitutes the name parameter’s value for the %v format verb. This will generate the greeting message based on the input name.
  • Finally, we return the message from the function.

4 – Call Golang Function from Another Module

Our next step is to use the greeting-module from another module and call the HelloGreeting function.

We will use the example from our Golang Hello World to call the greeting-module. See below example on how to make the call.

package main

import (
	"fmt"

	"progressivecoder.com/greeting"
)

func main() {
	message := greeting.HelloGreeting("Progressive Coder")
	fmt.Println(message)
}

As you can see, we have the main package here. In Go, code executed as an application must have a main package.

We also import the standard fmt package and our new greeting package. Importing a package gives us access to the exported functions within the package. Finally, we call the HelloGreeting function with name as Progressive Coder and then, print the message to the console.

5 – Setting the Dependencies for the Module

At this point, we have written the code. However, the Hello program still cannot access the greeting module. It basically does not know from where to download this module.

For production, we usually keep our reusable modules in a central location from where they can be downloaded. However, in this case, we want the hello.go program to use the module from our local file system.

To enable this we have to issue the below commands.

$ go mod edit -replace progressivecoder.com/greeting=../greeting-module
$ go mod tidy

The go.mod file will appear as below:

module example/hello

go 1.17

require progressivecoder.com/greeting v0.0.0-00010101000000-000000000000

replace progressivecoder.com/greeting => ../greeting-module

Basically, we are telling the Hello module to look for the greeting module in the ../greeting-module path on the local file system. Also, the command added the require directive to specify that example/hello requires the greeting-module. The number after the path is a pseudo version number

For this path to work, the hello module and the greeting-module should be located in the same directory. Else, you have to adjust the path accordingly.

/GoProjects/
  | -- hello
  | -- greeting-module

Finally, we can simply run our code using the below command in the hello directory. It will print the greeting message.

$ go run .
Hello Progressive Coder

Conclusion

With this, we have learnt how to create our first Go module, implement a function and how to call Golang function from another module or directory locally. We also looked at how to manage dependencies in this case.

If you have any comments or queries till this point, please feel free to mention in the comments section below.


Saurabh Dashora

Saurabh is a Software Architect with over 12 years of experience. He has worked on large-scale distributed systems across various domains and organizations. He is also a passionate Technical Writer and loves sharing knowledge in the community.

0 Comments

Leave a Reply

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