VOOZH about

URL: https://thenewstack.io/import-and-use-a-third-party-package-in-golang/

⇱ Import and Use a Third-Party Package in Golang - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-03-28 17:00:26
Import and Use a Third-Party Package in Golang
Software Development

Import and Use a Third-Party Package in Golang

The Go programming language has an excellent library of packages that offer a full range of functionality that you can use in your own Go programs.
Mar 28th, 2024 5:00pm by Jack Wallen
👁 Featued image for: Import and Use a Third-Party Package in Golang
Feature image via Go.dev.
Like most compiled programming languages, the Go programming language makes it possible to use external libraries and other pre-packaged tools.  For example, with the tried and true Hello World application, we call the main package and import the fmt package like this:
package main

import "fmt"

func main() {
    fmt.Println("Hello, New Stack")
}
Without the ability to call those packages, you’d have to write every single line of code, which would vastly complicate the process. That simple five-line application could all of a sudden turn into a hundred-line application. Given the goal is to work smarter and not harder, you do not want to have to deal with all of that extra programming — especially since it’s already been done. And, in the case of main and fmt, they’re built into the Golang application.  Handy. The main package is special because it is used to make a program executable. By using main, you’re telling the Go compiler that the program should be compiled as an executable and not a library. The fmt package is important because it allows the formatting of basic strings and values such that they can be printed to the standard output, collect user input from the console, or write input to a file. There are a wide array of possible packages that can be used for Golang. You can go to the Golang package search tool and search through the repository of packages. Let’s say, for example, you want to write a Go application that will print out a random, pithy quote. You might think that would be challenging but since there’s already a package for this, you don’t have to worry about coding everything for the app. What you do have to take care of before you can successfully compile your application is adding the third-party package. Fortunately, that’s not much of a challenge — you just have to know how it’s done. Let me show you. First, let me show you the application we’re going to run. Create a new .go file with the command:
nano quote.go
In that file, paste the following:
package main

import "fmt"

import "rsc.io/quote"

func main() {
 fmt.Println("Your pithy quote of the day is:\n")
 fmt.Println(quote.Go())

}
Once compiled, the above application will print the line: Don’t communicate by sharing memory, share memory by communicating. If you were to attempt to compile the above without first adding the quote package, you’d receive an error. How do you get around that? You first have to initialize the quote package, which is done with the command:
go mod init quote
Once you’ve done that, you can then download the package with:
go mod tidy
Now, compile your application with:
go build quote.go
This will create an executable named call. Run the app with:
./quote
You’ll see the output I listed above. Let’s go a bit deeper.  What we’re going to do now is create our own module and then call it from an application. First, create a new directory structure with the command:
mkdir -p projects/mymodule
Change into the modules directory with:
cd projects/mymodule
Initialize modules with:
go mod init mymodule
You’ll find a new go.mod file has been added. Next, create a main.go file with:
nano main.go
In that file, paste the following code:
package main

import "fmt"

func main() {
fmt.Println("Hello, New Stack!")
}
Pretty straightforward. Save and close the file.  You can now test-run the initial code with:
go run main.go
You should see the following output: Hello, New Stack! Great. Now, we’re going to create a new package that we’ll then call from our main.go file. Still, in the mymodule directory, create a new directory with:
mkdir mypackage
Create a new .go file with:
nano mypackage/mypackage.go
In that file, paste the following:
package newpackage

import "fmt"

func NSHello() {
             fmt.Println("Hello, New Stack! You've just called your first module!")
}
As you can see, we’ve created a function, called NSHello() which prints the line “Hello, New Stack!” You’ve just called your first module! Save and close the file.  Next, we need to call our new package from within the main.go file. Open that file for editing with the command:
nano /main.go
Currently, the file looks like this:
package main

import "fmt"

func main() {
        fmt.Println("Hello, New Stack!")
}
What we have to do is import the new package we just created. First, change the import section to:
import (
       "fmt"
       "mymodule/mypackage"
)
Next, change the func main() section to look like this:
func main() {
        fmt.Println("Hello, New Stack!")

        mypackage.NSHello()
}
What we’ve done in this file is import our new package (with the line mymodule/mypackage) and then use it in conjunction with our NSHello() function we defined in mypackage.go The entire file looks like this:
package main

import (
       "fmt"
       "mymodule/mypackage"
)

func main() {
        fmt.Println("Hello, New Stack!")

        mypackage.PrintHello()
}
Save and close the file. Run the entire application with:
go run main.go
The output should be: Hello, New Stack! Hello, New Stack! You’ve just called your first module! Or, you can build an executable with the command:
go build main.go
And that is your introduction to calling external packages in Golang.
TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.