WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Go · Basic · question 12 of 100

What is a package in Go? How do you import them?

📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

A package in Go is a collection of source files in the same directory that are compiled together. Packages provide a way to organize and reuse code, allowing the creation of modular and maintainable software. Each package defines a distinct namespace and should have a unique import path. A package can contain functions, constants, variables, and types.

To import a package in Go, you need to use the ‘import‘ keyword followed by the package’s import path enclosed in double quotes. You can import multiple packages by enclosing them in parentheses. Here’s an example of how to import the "fmt" and "math" packages:

package main

import (
    "fmt"
    "math"
)

func main() {
    fmt.Printf("The square root of 64 is: %vn", math.Sqrt(64))
}

In this example, we import the "fmt" package for formatted I/O and the "math" package for mathematical functions. Then, in the main function, we call the ‘Printf‘ function from the "fmt" package and ‘Sqrt‘ function from the "math" package.

Here’s an explanation of package importing using a flowchart created with the pgfplots package:

In summary, a package in Go is a way to organize and reuse code through a collection of source files. You import packages using the ‘import‘ keyword followed by the package’s import path enclosed in double quotes. Packages enable code modularity and maintainability by providing separate namespaces for different functionalities.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Go interview — then scores it.
📞 Practice Go — free 15 min
📕 Buy this interview preparation book: 100 Go questions & answers — PDF + EPUB for $5

All 100 Go questions · All topics