
- Go - Home
- Go - Overview
- Go - Environment Setup
- Go - Program Structure
- Go - Basic Syntax
- Go - Data Types
- Go - Variables
- Go - Constants
- Go - Identifiers
- Go - Keywords
- Go - Operators
- Go - Arithmetic Operators
- Go - Assignment Operators
- Go - Relational Operators
- Go - Logical Operators
- Go - Bitwise Operators
- Go - Miscellaneous Operators
- Go - Operators Precedence
- Go Decision Making
- Go - Decision Making
- Go - If Statement
- Go - If Else Statement
- Go - Nested If Statements
- Go - Switch Statement
- Go - Select Statement
- Go Control Flow Statements
- Go - For Loop
- Go - Nested for Loops
- Go - Break Statement
- Go - Continue Statement
- Go - Goto Statement
- Go Functions
- Go - Functions
- Go - Call by Value
- Go - Call by Reference
- Go - Functions as Values
- Go - Function Closure
- Go - Function Method
- Go - Anonymous function
- Go Strings
- Go - Strings
- Go - String Length
- Go - String Concatenation
- Go - Compare Strings
- Go - Split String
- Go - Substring Extraction
- Go - String Replacement
- Go - String Interpolation
- Go - Parse Date Strings
- Go Arrays
- Go - Arrays
- Go - Multidimensional Arrays
- Go - Multidimensional Arrays
- Go - Passing Arrays to Functions
- Go - Pointers
- Go - Pointers
- Go - Array of pointers
- Go - Pointer to pointer
- Go - Passing pointers to functions
- Go Advanced Control Structures
- Go - Scope Rules
- Go - Dereferencing Pointer
- Go - Structures
- Go - Slice
- Go - Slice of Slices
- Go - Range
- Go - Maps
- Go - Recursion
- Go - Type Casting
- Go - Interfaces
- Go - Type Assertion
- Go - Error Handling
- Go - Concurrency
- Go - Regular Expression
- Go - Inheritance
- Go - Packages
- Go - Templates
- Go - Reflection
- Go - Generics
- Go File Handling
- Go - Read File By Word
- Go - Read File By Line
- Go - Read CSV Files
- Go - Delete File
- Go - Rename & Move File
- Go - Truncate a File
- Go - File Read-Write Mode W/O Truncation
- Go Miscellaneous
- Go - defer Keyword
- Go - Fmt Package
- Go - Zero Value
- Go - Import
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