
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:
<span style="font-weight: 400;">nano quote.go</span>
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:
<span style="font-weight: 400;">go mod init quote</span>
Once you’ve done that, you can then download the package with:
<span style="font-weight: 400;">go mod tidy</span>
Now, compile your application with:
<span style="font-weight: 400;">go build quote.go</span>
This will create an executable named call. Run the app with:
<span style="font-weight: 400;">./quote</span>
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:
<span style="font-weight: 400;">mkdir -p projects/mymodule</span>
Change into the modules directory with:
<span style="font-weight: 400;">cd projects/mymodule</span>
Initialize modules with:
<span style="font-weight: 400;">go mod init mymodule</span>
You’ll find a new go.mod file has been added.
Next, create a main.go file with:
<span style="font-weight: 400;">nano main.go</span>
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:
<span style="font-weight: 400;">go run main.go</span>
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:
<span style="font-weight: 400;">mkdir mypackage</span>
Create a new .go file with:
<span style="font-weight: 400;">nano mypackage/mypackage.go</span>
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:
<span style="font-weight: 400;">nano /main.go</span>
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:
<span style="font-weight: 400;">go run main.go</span>
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:
<span style="font-weight: 400;">go build main.go</span>
And that is your introduction to calling external packages in Golang.
The post Import and Use a Third-Party Package in Golang appeared first on The New Stack.
Post a Comment
0Comments