📖 Scenario: Imagine you are building a small program in Go that needs to organize code into parts. Using packages helps keep your code neat and easy to manage, just like putting tools in labeled boxes.
🎯 Goal: You will create a simple Go program that uses a package to organize a greeting message. This will show why packages are useful for grouping related code.
📋 What You'll Learn
Create a package named greetings with a function Hello that returns a greeting string
Create a main program that imports the greetings package
Call the Hello function from the greetings package and print the result
💡 Why This Matters
🌍 Real World
In real software projects, packages help teams organize code by features or functionality, making it easier to work together and keep code clean.
💼 Career
Understanding packages is essential for Go developers because it is the main way to structure programs and share code across projects.
Progress0 / 4 steps
1
Create the greetings package
Create a Go file with package name greetings. Inside it, write a function called Hello that returns the string "Hello, welcome to Go packages!".
Go
Hint
Start with package greetings at the top. Then write a function Hello that returns the greeting string.
2
Create the main program
Create a Go file with package name main. Import the greetings package you created in Step 1.
Go
Hint
Use package main and import the greetings package with import "greetings".
3
Call the Hello function
In the main function, call greetings.Hello() and store the result in a variable called message.
Go
Hint
Inside main(), write message := greetings.Hello() to get the greeting.
4
Print the greeting message
Use fmt.Println to print the message variable inside the main function.
Go
Hint
Print the message with fmt.Println(message) to see the greeting on the screen.
Practice
(1/5)
1. Why do Go programmers use packages in their code?
easy
A. To organize code into reusable parts
B. To make the program run faster
C. To write code without any functions
D. To avoid using variables
Solution
Step 1: Understand the purpose of packages
Packages help group related code together so it can be reused easily.
Step 2: Compare options with package purpose
Only organizing code into reusable parts matches the main reason for using packages.
Final Answer:
To organize code into reusable parts -> Option A
Quick Check:
Packages organize code = A [OK]
Hint: Packages group code for reuse and clarity [OK]
Common Mistakes:
Thinking packages speed up program execution
Believing packages remove the need for functions
Assuming packages eliminate variables
2. Which of the following is the correct way to declare a package in a Go file?
easy
A. func main()
B. import main
C. package main
D. var main
Solution
Step 1: Recall Go package declaration syntax
In Go, the first line usually declares the package using the keyword 'package' followed by the package name.
Step 2: Identify correct syntax among options
Only 'package main' correctly declares a package; others are for imports, functions, or variables.
Final Answer:
package main -> Option C
Quick Check:
Package declaration starts with 'package' keyword = C [OK]
Hint: Package declaration starts with 'package' keyword [OK]
Common Mistakes:
Using 'import' instead of 'package' to declare package
Confusing function declaration with package declaration
Trying to declare package with 'var'
3. What will be the output of this Go program?
package main
import "fmt"
func main() {
fmt.Println("Hello from package main")
}
medium
A. Hello from package main
B. package main
C. fmt.Println("Hello from package main")
D. Compilation error
Solution
Step 1: Understand the program structure
The program imports fmt package and calls fmt.Println to print a string.
Step 2: Predict the output of fmt.Println
fmt.Println prints the string inside quotes exactly to the console.
Final Answer:
Hello from package main -> Option A
Quick Check:
Print statement outputs string = A [OK]
Hint: fmt.Println prints the string inside quotes [OK]
Common Mistakes:
Confusing package name with output
Expecting code line as output
Assuming compilation error without reason
4. This Go code has an error. What is the problem?