Bird
Raised Fist0
Goprogramming~20 mins

Creating custom packages in Go - Practice Exercises

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

Practice

(1/5)
1. What is the main purpose of creating a custom package in Go?
easy
A. To organize code into reusable parts
B. To make the program run faster
C. To avoid using functions
D. To write code without imports

Solution

  1. Step 1: Understand the role of packages

    Packages help group related code together for reuse and clarity.
  2. Step 2: Identify the main benefit

    Custom packages allow you to reuse code in different parts of your program or in other programs.
  3. Final Answer:

    To organize code into reusable parts -> Option A
  4. Quick Check:

    Custom packages = reusable code [OK]
Hint: Packages group code for reuse and clarity [OK]
Common Mistakes:
  • Thinking packages speed up code execution
  • Believing packages remove the need for functions
  • Assuming packages eliminate imports
2. Which of the following is the correct way to export a function from a custom package in Go?
easy
A. func MyFunction() {}
B. func my_function() {}
C. func _MyFunction() {}
D. func myFunction() {}

Solution

  1. Step 1: Recall Go export rules

    In Go, only names starting with an uppercase letter are exported (visible outside the package).
  2. Step 2: Check function names

    Only 'MyFunction' starts with uppercase, so it is exported.
  3. Final Answer:

    func MyFunction() {} -> Option A
  4. Quick Check:

    Uppercase start = exported function [OK]
Hint: Exported names start with uppercase letters [OK]
Common Mistakes:
  • Using lowercase to export functions
  • Using underscores to export
  • Assuming snake_case exports functions
3. Given a package named mathutil with an exported function Square, what is the output of this code?
package main

import (
  "fmt"
  "mathutil"
)

func main() {
  fmt.Println(mathutil.Square(4))
}
medium
A. Square(4)
B. 16
C. Error: undefined mathutil
D. 0

Solution

  1. Step 1: Understand the function call

    The code calls mathutil.Square(4), which should return 4 squared (4*4).
  2. Step 2: Calculate the expected output

    4 multiplied by 4 equals 16, so the output is 16.
  3. Final Answer:

    16 -> Option B
  4. Quick Check:

    Square(4) = 16 [OK]
Hint: Square means multiply number by itself [OK]
Common Mistakes:
  • Assuming function name prints as string
  • Thinking import fails without path
  • Expecting zero as default output
4. What is wrong with this custom package usage?
package main

import (
  "fmt"
  "mypkg"
)

func main() {
  fmt.Println(mypkg.helper())
}

Assuming helper is a function in mypkg.
medium
A. Package name must be Main to import
B. fmt.Println cannot print function results
C. Import path must be a URL
D. Function name should start with uppercase to be accessible

Solution

  1. Step 1: Check function visibility rules

    In Go, only functions starting with uppercase letters are exported and accessible outside their package.
  2. Step 2: Identify the function name issue

    The function is named 'helper' (lowercase), so it is not accessible from main package.
  3. Final Answer:

    Function name should start with uppercase to be accessible -> Option D
  4. Quick Check:

    Lowercase function = not exported [OK]
Hint: Exported functions start uppercase [OK]
Common Mistakes:
  • Thinking package name must be Main
  • Believing import path must be URL
  • Assuming fmt.Println can't print function output
5. You want to create a custom package stringutil with a function Reverse that reverses a string. Which of the following is the correct way to define and use it in your main.go?
hard
A. In stringutil package: func Reverse(s string) string { /* code */ }
In main.go: import "stringutil" and call Reverse("hello")
B. In stringutil package: func reverse(s string) string { /* code */ }
In main.go: import "stringutil" and call stringutil.reverse("hello")
C. In stringutil package: func Reverse(s string) string { /* code */ }
In main.go: import "stringutil" and call stringutil.Reverse("hello")
D. In stringutil package: func Reverse(s string) string { /* code */ }
In main.go: no import needed, call Reverse("hello")

Solution

  1. Step 1: Define exported function in package

    The function must start with uppercase 'Reverse' to be accessible outside the package.
  2. Step 2: Import package and call function with package name

    In main.go, import "stringutil" and call the function as stringutil.Reverse("hello").
  3. Final Answer:

    Define func Reverse and call stringutil.Reverse("hello") after import -> Option C
  4. Quick Check:

    Uppercase function + import + package prefix = correct usage [OK]
Hint: Exported func uppercase + import + package.func call [OK]
Common Mistakes:
  • Using lowercase function name and expecting export
  • Calling function without package prefix after import
  • Not importing the custom package
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