Bird
Raised Fist0
Goprogramming~10 mins

Importing packages in Go - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the "fmt" package.

Go
package main

import [1]

func main() {
    fmt.Println("Hello, Go!")
}
Drag options to blanks, or click blank then click option'
A"math"
B"os"
C"fmt"
D"net/http"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting the quotes around the package name.
Importing the wrong package like "os" instead of "fmt".
2fill in blank
medium

Complete the code to import both "fmt" and "math" packages.

Go
package main

import (
    [1]
    "math"
)

func main() {
    fmt.Println(math.Sqrt(16))
}
Drag options to blanks, or click blank then click option'
A"os"
B"fmt"
C"net/http"
D"strings"
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the "fmt" package causes an error when calling fmt.Println.
Using incorrect package names.
3fill in blank
hard

Fix the error in the import statement to correctly import the "net/http" package.

Go
package main

import (
    "fmt"
    [1]
)

func main() {
    resp, err := http.Get("http://example.com")
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(resp.Status)
}
Drag options to blanks, or click blank then click option'
A"net/http"
Bnet/http
C'net/http'
Dnet_http
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes.
Writing package names without quotes.
4fill in blank
hard

Fill both blanks to import "fmt" and alias "math/rand" as "random".

Go
package main

import (
    [1]
    [2]
)

func main() {
    fmt.Println(random.Intn(10))
}
Drag options to blanks, or click blank then click option'
A"fmt"
B"math/rand"
Crandom "math/rand"
D"os"
Attempts:
3 left
💡 Hint
Common Mistakes
Not using alias syntax correctly.
Importing "math/rand" without alias when alias is needed.
5fill in blank
hard

Fill all three blanks to import "fmt", alias "math/rand" as "random", and import "time" package.

Go
package main

import (
    [1]
    [2]
    [3]
)

func main() {
    fmt.Println(random.Intn(100))
    fmt.Println(time.Now())
}
Drag options to blanks, or click blank then click option'
A"fmt"
Brandom "math/rand"
C"time"
D"strings"
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting quotes around package names.
Not aliasing "math/rand" correctly.
Missing one of the packages.

Practice

(1/5)
1. What is the correct way to import a single package named fmt in Go?
easy
A. include fmt
B. import "fmt"
C. using fmt
D. require "fmt"

Solution

  1. Step 1: Understand Go import syntax

    In Go, packages are imported using the keyword import followed by the package name in double quotes.
  2. Step 2: Check each option

    Only import "fmt" uses the correct syntax: import "fmt". Other options use incorrect keywords or missing quotes.
  3. Final Answer:

    import "fmt" -> Option B
  4. Quick Check:

    Go import single package = import "package" [OK]
Hint: Use import with quotes around package name [OK]
Common Mistakes:
  • Forgetting quotes around package name
  • Using wrong keywords like include or require
  • Missing import keyword
2. Which of the following is the correct syntax to import multiple packages fmt and math in Go?
easy
A. import ( "fmt" "math" )
B. import ("fmt", "math")
C. import "fmt" "math"
D. import fmt, math

Solution

  1. Step 1: Recall multi-package import syntax

    In Go, multiple packages are imported inside parentheses with each package name in quotes on separate lines.
  2. Step 2: Analyze options

    import ( "fmt" "math" ) correctly uses parentheses and places each package name on its own line with quotes. Options B, C, and D are incorrect syntax.
  3. Final Answer:

    import ( "fmt" "math" ) -> Option A
  4. Quick Check:

    Multiple imports use parentheses and quotes [OK]
Hint: Use parentheses and quotes for multiple imports [OK]
Common Mistakes:
  • Missing parentheses for multiple imports
  • Not putting package names in quotes
  • Separating packages with commas instead of new lines
3. What will be the output of this Go program?
package main
import (
  "fmt"
  "math"
)
func main() {
  fmt.Println(math.Sqrt(16))
}
medium
A. Error: undefined math.Sqrt
B. 4
C. 16
D. 4.0

Solution

  1. Step 1: Understand the code

    The program imports fmt and math packages. It calls math.Sqrt(16) which returns the square root of 16 as a float64.
  2. Step 2: Determine output type

    math.Sqrt(16) returns 4.0 (float64). fmt.Println prints it as 4.0 including the decimal point because it is a float64 value.
  3. Final Answer:

    4.0 -> Option D
  4. Quick Check:

    math.Sqrt(16) = 4.0 [OK]
Hint: math.Sqrt returns float64, prints as 4.0 including decimal [OK]
Common Mistakes:
  • Expecting integer output instead of float64
  • Thinking math package is not imported
  • Confusing output with error
4. Identify the error in this Go code snippet:
package main
import "fmt"
import "math"
func main() {
  fmt.Println(math.Pow(2, 3))
}
medium
A. Cannot import multiple packages separately
B. Missing parentheses for multiple imports
C. No error, code runs fine
D. math.Pow does not exist

Solution

  1. Step 1: Check import statements

    Go allows multiple import statements separately or grouped in parentheses. Both are valid.
  2. Step 2: Verify function usage

    math.Pow(2, 3) is a valid function call to calculate 2 to the power 3.
  3. Final Answer:

    No error, code runs fine -> Option C
  4. Quick Check:

    Separate imports allowed = no error [OK]
Hint: Multiple import statements are allowed separately [OK]
Common Mistakes:
  • Thinking multiple imports must be in parentheses
  • Assuming math.Pow does not exist
  • Confusing import syntax rules
5. You want to import the strings package but use it with the name str in your code. Which import statement is correct?
hard
A. import str "strings"
B. import "str" as "strings"
C. import strings as str
D. import str from "strings"

Solution

  1. Step 1: Understand alias import syntax in Go

    To use a package with a different name, Go allows aliasing by writing the alias before the package name in quotes.
  2. Step 2: Check options

    import str "strings" uses correct syntax: import str "strings". Other options use invalid syntax not supported in Go.
  3. Final Answer:

    import str "strings" -> Option A
  4. Quick Check:

    Alias import = alias before package in quotes [OK]
Hint: Alias package by writing alias before quoted package name [OK]
Common Mistakes:
  • Using 'as' keyword which Go does not support
  • Trying to import with 'from' keyword
  • Placing alias after package name
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard