0
0
Goprogramming~10 mins

Importing packages in Go - Interactive Code Practice

Choose your learning style9 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.