Complex programs that do something useful are often built up by calling functions from various packages or libraries. Golang is no different. In this post, we will learn how to import Golang External Package in our program.

In case you are new to Go programming language, I would recommend you to start with Golang Hello World and then return to this post.

1 – Understanding the Demo Package

We already looked at printing a standard Hello, World message.

However, what if we want to print something more interesting coming from an external source?

We can use the rsc.io/quote package. Basically, it provides a bunch of functions that return some proverbial sayings about life. Sounds perfect for our requirement!

You can search for various packages on https://pkg.go.dev/. Think of it as a repository of packages just like NPM is for NodeJS.

Important thing to understand is that packages are published in modules. In this case rsc.io/quote is a module that others can use. Over time, you can add new versions of your module to enhance the functionality.

2 – Import Golang External Package

In order to use the module, we have to import it in our Go program and then call an appropriate function within that module.

Below is the how we can do it in our hello.go program from the Golang Hello World post.

package main

import (
	"fmt"

	"rsc.io/quote"
)

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

We have the import statement to import the rsc.io/quote package. Also, we call the Opt() function in the quote module and pass the same to the Println() function of the fmt module.

And that’s about it from a code point of view.

However, if we run our hello.go program now, we will get the below error:

hello.go:6:2: no required module provides package rsc.io/quote; to add it:
	go get rsc.io/quote

Basically, the compiler is not able to find the rsc.io/quote package.

3 – Adding the Golang External Package

In order to make Go aware of the external package, we need to execute the below command.

$ go mod tidy
go: finding module for package rsc.io/quote
go: found rsc.io/quote in rsc.io/quote v1.5.2

The above command will add the quote module as a requirement. In other words, it will add the dependency in the go.mod file. See below:

module example/hello

go 1.17

require rsc.io/quote v1.5.2

require (
	golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c // indirect
	rsc.io/sampler v1.3.0 // indirect
)

As you can see, the require section in the mod file is pointing to the quote package. Also, the above command will create a go.sum file. It is used for authenticating the module.

4 – Running the Program

Finally, we can run the program using the below command:

$ go run .
Hello, World
If a program is too slow, it must have a loop.

As you can see, the Hello, World is printed. This is followed by a quote from the quote package.

Conclusion

With this, we have successfully learnt how to import Golang External Package. We learnt how to import an external package and also use the go mod tidy command to manage the dependencies.

If you have any comments or queries about this, please feel free to write 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 *