Import a library/package

Learn via video courses
Topics Covered

Overview

Packages are the most important part of the Go language since it is used to design and maintain a large number of programs in a single unit for easy access and useability of the code. This type of modularity allows the code to be shared and reused. Every package in the Go language is defined with a different name.

Introduction

Packages are used to organize go source code for better reusability and readability. Packages offer compartmentalization of code and hence it becomes easy to maintain go applications. There are two types of packages in the Go language:

  • Executable Package- its main application is to execute it from the command prompt.
  • Utility Package- it is no-executable and its role is to provide a utility function.

Inbuilt Packages

The commonly used inbuilt packages in the Go language are:

  • fmt package
  • math package
  • string package

Golang fmt Package

It provides a function to format input and output data. Some of the fmt functions that are commonly used are

FunctionDescription
Print()It is used for printing the text on the output screen.
Println()It is used for printing the text output with a new line at the end.
Printf()It is used for printing the formatted string at the output.
Scan()It gets the input value from the user.
Scanf()It gets the input value from the user until the new line is detected.

Math Package in Go Language

This package provides various functions to perform mathematical operations. Some of the commonly used math functions are

FunctionDescription
Sqrt()It is used for returning the square root of a number.
Cbrt()It is used for returning the cube root of a number.
Max()It finds and returns the maximum number between the two.
Min()It finds and returns the minimum number between the two.
Mod()It is used for computing the remainder after the division.

String Package in Go Language

This package provides various functions that can be used for string manipulation. Some of the commonly used string methods are

FunctionsDescription
Compare()It checks if two strings are equal or not.
Contains()It is used for checking if a string contains a substring.
Count()It counts the number of times a substring is present in a string.
Join()It creates a new string by concatenating elements of a string array.
ToLower()It is used for converting a string to lowercase.
ToUpper()It is used for converting a string to uppercase.

Import Statement in Golang

Working of import:

  • To import a package we use import syntax followed by package name.
  • Go search the package directory inside GOROOT/src.
  • If not found then it search inside GOPATH/src.
  • Note: fmt is a part of Go’s standard library and is imported from GOROOT/src.

Types of Import

Direct

It supports the direct import of packages by using the following syntax: import "fmt" Both single and multiple packages can be imported by using this syntax.

Syntax

Example

Nested

It is supported by the Go language. As the name suggests, nested import is importing packages from that a larger package. For example, sometimes we just need one function from either package, in this situation importing the entire package for just one function increases the memory of code, and hence that single function is imported from the package.

Syntax

Example

Grouped

In this, there is no need of writing an import keyword multiple times, instead, one import keyword along with round braces works().

Syntax

Example

Aliased

It is a very useful feature of Go’s import statement. A common use case for import aliases is to provide a shorter alternative to a library’s package name.

Syntax

Example

Blank

If you have ever been annoyed at Go complaining about an unused import, then you have most likely come across blank imports. Blank imports are commonly used while coding to stop Go complaining about an import that you are not using but might use later, that you don’t want to keep around for when you do need it.

Syntax

Example

Dot

Dot imports are lesser known and therefore rarely used import methods. What it does is it imports the package into the same namespace as the current package, so you no longer have to use the imported package’s name to reference its variable and functions, giving us direct access to them. To dot import a package, you simply give it a full stop as it’s an alias.

Syntax

Example

Relative

When the package is created and placed in a Go’s directory, a path is formed which is known as $GOPATH. While using it, you mention the path of the directory where it is stored. This type of import is known as a relative import.

Syntax

Example

Circular

A circular import is similar to that of loops in a programming language. This means that it defines the package that is imported and defines the second package implicitly such that it imports the first package.

Example

Installing Third-party Packages

In order to install a third-party package, download and install it from the source repository by using the command, go get. This command will help fetch the packages the from he source repository and will install it on the GOPATH directory. It can be imported by using the import keyword.

Example

This command will install the yaml package in the directory. Once the package is installed, use the import statement in order to import it from the directory and to use it in the code.

Upgrade Third-party Package Version

For upgrading the third-party package in the Go lang application, use the command, go get package_name for downloading and installing the latest version of the package. For upgrading the existing package, use the command, go get -u all to update all the packages to their latest version.

Conclusion

  • Packages are the most important part of the Go language since it is used to design and maintain a large number of programs in a single unit for easy access and useability of the code. This type of modularity allows the code to be shared and reused.
  • There are two types of packages in the Go language:
    • Executable package- Its main application is to execute it from the command prompt.
    • Utility package- It is no-executable and its role is to provide utility function.
  • The commonly used inbuilt packages in the Go language are:
    • fmt package
    • math package
    • string package
  • In order to install a third-party package, download and install it from the source repository by using the command, go get.
  • Use the command, go get -u all to update all the packages to their latest version.