Chapter 4 - Packages
Exercise 1: Importing Packages
We’ve set up a Go package at https://github.com/jaymcgavren/car
.
What command would you run in your terminal to download and install this package in your Go workspace?
$
Once you’ve installed the package, you’ll have the following files in your workspace:
go/src/github.com/jaymcgavren/car/car.go
package car
import "fmt"
func openDoor() {
fmt.Println("Opening door")
}
go/src/github.com/jaymcgavren/car/headlights/headlights.go
package headlights
import "fmt"
func TurnOn() {
fmt.Println("Shining headlights")
}
go/src/github.com/jaymcgavren/car/stereo/amplifier.go
package stereo
import "fmt"
func TurnOn() {
fmt.Println("Playing music")
}
go/src/github.com/jaymcgavren/car/stereo/speakers.go
package stereo
import "fmt"
func BoostBass() {
fmt.Println("Bwomp bwamp BWOOOOMP")
}
go/src/github.com/jaymcgavren/car/wheels/wheels.go
package wheels
import "fmt"
func Accelerate() {
fmt.Println("Peeling out!")
}
func Steer() {
fmt.Println("Turning front wheels")
}
Write a program that imports all the above packages and calls all the functions they contain. You can include the program in your Go workspace, if you like, or just store it anywhere and run it with go run
.
The program output should look like this:
Opening door
Shining headlights
Playing music
Bwomp bwamp BWOOOOMP
Turning front wheels
Peeling out!
When you’re ready, have a look at the solution.