0
0
Goprogramming~20 mins

Creating custom packages in Go - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Go Custom Package Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple custom package usage
What is the output of this Go program that uses a custom package named greet?
Go
package main

import (
	"fmt"
	"example.com/mypkg/greet"
)

func main() {
	message := greet.Hello("Alice")
	fmt.Println(message)
}

// Package greet in example.com/mypkg/greet/greet.go
package greet

func Hello(name string) string {
	return "Hello, " + name + "!"
}
AHello Alice
BError: undefined greet.Hello
Chello, Alice!
DHello, Alice!
Attempts:
2 left
💡 Hint
Check how the Hello function formats the string and if it includes punctuation.
🧠 Conceptual
intermediate
1:30remaining
Understanding package visibility rules
In Go, which of the following statements about identifiers in custom packages is true?
AIdentifiers starting with a lowercase letter are accessible outside the package.
BOnly identifiers starting with an uppercase letter are exported and accessible outside the package.
CAll identifiers in a package are accessible outside the package regardless of case.
DIdentifiers starting with an underscore are exported automatically.
Attempts:
2 left
💡 Hint
Think about how Go controls what is visible outside a package.
🔧 Debug
advanced
2:30remaining
Fix the import path error in custom package usage
Given this main.go file, what is the cause of the error when trying to build the program? main.go: package main import ( "fmt" "mypkg/greet" ) func main() { fmt.Println(greet.Hello("Bob")) } The error is: cannot find package "mypkg/greet"
AThe greet package is not in the same directory as main.go.
BThe greet package does not have a Hello function exported.
CThe import path "mypkg/greet" is incorrect; it should be a full module path or relative path.
DThe main package is missing a main function.
Attempts:
2 left
💡 Hint
Check how Go modules and import paths work for custom packages.
📝 Syntax
advanced
1:30remaining
Identify the syntax error in custom package declaration
Which option shows the correct syntax for declaring a custom package named mathutil in Go?
A
package mathutil

func Add(a int, b int) int {
	return a + b
}
B
package MathUtil

func Add(a int, b int) int {
	return a + b
}
C
package mathutil {

func Add(a int, b int) int {
	return a + b
}
}
D
package mathutil

func Add(a int, b int) int {
return a + b
}
Attempts:
2 left
💡 Hint
Remember how package declarations are written in Go.
🚀 Application
expert
2:00remaining
Number of exported functions in a custom package
Consider a Go package named tools with the following functions: func ExportedOne() {} func exportedTwo() {} func ExportedThree() {} func exportedFour() {} How many functions are accessible (exported) when importing the tools package in another Go file?
A2
B0
C4
D3
Attempts:
2 left
💡 Hint
Recall Go's rule for exported identifiers based on capitalization.