Go Exported Names Challenge: Building a Public API
Go's visibility rules are fundamental to creating well-structured and maintainable packages. This challenge focuses on understanding and correctly implementing exported names (public API) in Go packages. You'll be designing a simple package and ensuring that only the intended functions and variables are accessible from outside the package.
Problem Description
You are tasked with creating a package named geometry that provides basic geometric calculations. This package should contain functions for calculating the area of a rectangle and the circumference of a circle. However, you must carefully control which functions are part of the public API and which are internal implementation details. Specifically, you need to ensure that only the RectangleArea and CircleCircumference functions are accessible from outside the geometry package. Any helper functions or variables used internally within the geometry package should not be exported.
Key Requirements:
- Create a package named
geometry. - Implement a function
RectangleArea(length, width float64) float64that calculates and returns the area of a rectangle. - Implement a function
CircleCircumference(radius float64) float64that calculates and returns the circumference of a circle. - Ensure that only
RectangleAreaandCircleCircumferenceare exported (accessible from other packages). - Include at least one unexported (private) function or variable within the
geometrypackage to demonstrate understanding of visibility rules.
Expected Behavior:
When another package imports the geometry package, it should only be able to call RectangleArea and CircleCircumference. Attempts to access any other functions or variables within the geometry package from outside should result in a compile-time error.
Edge Cases to Consider:
- What happens if you try to export a variable? (It's generally discouraged, but good to understand the rules).
- How does capitalization affect visibility in Go? (This is the core concept to master).
Examples
Example 1:
Input: geometry.RectangleArea(5.0, 10.0)
Output: 50.0
Explanation: The RectangleArea function correctly calculates the area of a rectangle with length 5.0 and width 10.0.
Example 2:
Input: geometry.CircleCircumference(3.0)
Output: 18.84955592153876
Explanation: The CircleCircumference function correctly calculates the circumference of a circle with radius 3.0.
Example 3: (Demonstrating a compile-time error)
package main
import (
"fmt"
"geometry"
)
func main() {
area := geometry.RectangleArea(4.0, 6.0)
fmt.Println(area)
// This should cause a compile-time error because 'internalHelper' is not exported
// fmt.Println(geometry.internalHelper(10))
}
Explanation: The commented-out line will fail to compile because internalHelper is not exported from the geometry package.
Constraints
- The
geometrypackage must be in a separate file namedgeometry.go. - The
mainpackage (where you test thegeometrypackage) can be in a file namedmain.go. - The code must compile and run without errors.
- The
RectangleAreafunction must return the correct area (length * width). - The
CircleCircumferencefunction must return the correct circumference (2 * pi * radius, where pi ≈ 3.14159). - The solution should be concise and readable.
Notes
- Remember that Go's visibility rules are based on capitalization. Functions and variables starting with a capital letter are exported, while those starting with a lowercase letter are not.
- Consider creating a helper function within the
geometrypackage that is not exported to demonstrate your understanding of visibility. This helper function doesn't need to be used byRectangleAreaorCircleCircumference, but its presence confirms you can control export. - Think about how you would structure your
geometry.gofile to clearly separate public and private elements.