0
0
Goprogramming~30 mins

Why packages are used in Go - See It in Action

Choose your learning style9 modes available
Why packages are used
📖 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.