📖 Scenario: You are building a small Go program that uses a custom package to organize your code better. This is like having a toolbox with different compartments for different tools.
🎯 Goal: Create a custom package named greetings that has a function to return a welcome message. Then use this package in your main program to print the message.
📋 What You'll Learn
Create a package named greetings in a separate file
Inside greetings, write a function WelcomeMessage that returns a string
In the main program, import the greetings package
Call greetings.WelcomeMessage() and print its result
💡 Why This Matters
🌍 Real World
Custom packages help organize code into reusable parts, making programs easier to manage and understand.
💼 Career
Knowing how to create and use packages is essential for writing clean, maintainable Go programs in professional software development.
Progress0 / 4 steps
1
Create the greetings package
Create a file named greetings.go with a package called greetings. Inside it, write a function named WelcomeMessage that returns the string "Hello from the greetings package!".
Go
Hint
Remember to start with package greetings and define a function that returns the exact string.
2
Set up the main program
Create a file named main.go. Write the main package and import the fmt package and your custom greetings package.
Go
Hint
Use import with parentheses to include both fmt and greetings.
3
Call the WelcomeMessage function
Inside the main function, call greetings.WelcomeMessage() and store the result in a variable named message.
Go
Hint
Use := to declare and assign the variable message.
4
Print the welcome message
Use fmt.Println to print the variable message inside the main function.
Go
Hint
Use fmt.Println(message) to show the message on the screen.
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
Step 1: Understand the role of packages
Packages help group related code together for reuse and clarity.
Step 2: Identify the main benefit
Custom packages allow you to reuse code in different parts of your program or in other programs.
Final Answer:
To organize code into reusable parts -> Option A
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
Step 1: Recall Go export rules
In Go, only names starting with an uppercase letter are exported (visible outside the package).
Step 2: Check function names
Only 'MyFunction' starts with uppercase, so it is exported.
Final Answer:
func MyFunction() {} -> Option A
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?
D. Function name should start with uppercase to be accessible
Solution
Step 1: Check function visibility rules
In Go, only functions starting with uppercase letters are exported and accessible outside their package.
Step 2: Identify the function name issue
The function is named 'helper' (lowercase), so it is not accessible from main package.
Final Answer:
Function name should start with uppercase to be accessible -> Option D
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
Step 1: Define exported function in package
The function must start with uppercase 'Reverse' to be accessible outside the package.
Step 2: Import package and call function with package name
In main.go, import "stringutil" and call the function as stringutil.Reverse("hello").
Final Answer:
Define func Reverse and call stringutil.Reverse("hello") after import -> Option C