Go - Import



In Go, the import statement is used to include external packages(libraries or modules) in the program. The import statement is placed at the top of your Go program file, right after the package declaration. These packages provide additional requirements that you can use in your program.

What does import do and why do we use it?

The "import" tells the Go compiler that "I want to use this package in my program". Once imported, you can use the functions, variables, or types provided by that particular package.

Instead of writing the same code again and again, you can use pre-built packages because it provide ready-made solutions for common tasks like (e.g: printing, math operations, file handling, etc.).

Syntax

Following is the syntax of the Import in Golang −

import "package_name"

Basic Example

In this example, the fmt package is imported which provide functions for formatted I/O and uses its Println function to print "Hello, World!" to the console.

package main
import "fmt"
func main() {
   fmt.Println("Hello, World!")
}

Following is the output to the above program −

Hello, World!

Importing Multiple Packages

You can include and use multiple external packages in your program by grouping them inside parentheses after the import keyword.

Syntax

Following is the syntax of Importing Multiple Packages in Golang −

import (
   "fmt"
   "math"
)

Example of Multiple Packages

In this example, we import the fmt and math packages, then calculates and prints the square root of 225 using math.Sqrt.

package main
import (
   "fmt"
   "math"
)
func main() {
   fmt.Println("Square root of 225 is", math.Sqrt(225))
}

Following is the output to the above program −

Square root of 225 is 15

Aliasing Imports

You can use alias an imported package if you want to use a different name for it in your program.

Syntax

Following is the syntax of the Import in Golang −

import (
   "fmt"
   m "math"
)

Example of Aliasing Imports

In this example, we import the math package with an alias m and uses m.Sqrt to calculate the square root of 36.

package main
import (
   "fmt"
   m "math"
)
func main() {
   fmt.Println("Square root of 36 is", m.Sqrt(36))
}

Following is the output to the above program −

Square root of 36 is 6

Dot Import

A dot import allows you to use the package's exported identifiers without qualifying them with the package name

Note: Dot imports are discouraged because they can make the code harder to read and understand.

Syntax

Following is the syntax of Dot Import in Golang −

import . "fmt"

Example of Dot Import

In this example, we import the fmt package using a dot import, allowing direct use of Println without the fmt prefix.

package main
import . "fmt"
func main() {
    Println("Hello, World!") 
}

Following is the output to the above program −

Hello, World!

Blank Import

Sometimes, you might import a package for its side effects (e.g., initialization functions). In such cases, you can use a blank identifier _ to import the package without using it:

Syntax

Following is the syntax of the Blank Import in Golang −

import _ "image/png"

This is often used when the package registers itself with the Go runtime, such as image format handlers.

Example of Blank Import

In this example, we import the image/png package for its side effects (e.g: registering the PNG format) without using it, then prints a message.

package main
import (
   "fmt"
   _ "image/png"
)
func main() {
   fmt.Println("PNG format registered")
}

Following is the output to the above program −

PNG format registered
Advertisements