Write Your First Program
These foundational exercises introduce you to the basics of Go syntax and program structure.Print Names
Print Names
Objective: Practice using
fmt.Println to output textWrite a program that prints your name and your best friend’s name using fmt.Println twice.Key Concepts:- Using the
fmtpackage - Calling functions with string arguments
- Basic program structure
- Import the
fmtpackage - Create a
mainfunction - Use
fmt.Println()for each name
Build and Share
Build and Share
Print vs Println
Print vs Println
Objective: Understand the difference between Print and PrintlnReplace
fmt.Println with fmt.Print in your program and observe the difference.Key Concepts:PrintvsPrintlnbehavior- Newline characters
- Output formatting
Printlnadds a newline after outputPrintdoes not add a newline- Try calling each multiple times to see the difference
Multiple Values
Multiple Values
Objective: Learn to print multiple values in one callCall
Println or Print with multiple values separated by commas.Key Concepts:- Variadic functions
- Multiple arguments
- Space separation
Experiment with Syntax
Experiment with Syntax
Objective: Understand Go’s syntax requirementsTry these experiments to learn what works and what doesn’t:
- Remove double quotes from a string literal
- Move package and import statements to the bottom of the file
- String literal syntax
- File structure requirements
- Compiler error messages
Packages and Scopes
Understanding packages and scopes is crucial for organizing your Go code effectively.Create Your Own Package
Create Your Own Package
Objective: Learn to create and use custom packagesCreate multiple Go files with different packages and call their functions from main.Key Concepts:
- Package declaration
- Exported vs unexported identifiers
- Importing local packages
- File organization
- Create a new directory for your package
- Declare package with
package mypackage - Create exported functions (capitalized names)
- Import and use in your main package
Understanding Scopes
Understanding Scopes
Objective: Learn how scoping works across packagesExperiment with variable and function visibility across different scopes.Key Concepts:
- Package-level scope
- Function-level scope
- Block scope
- Exported vs unexported identifiers
- Create variables at different scope levels
- Try to access them from different locations
- Observe which identifiers are accessible where
- Remember: Capitalized = exported, lowercase = unexported
Rename Imports
Rename Imports
Objective: Handle package name conflictsImport the same package using different names (aliasing).Key Concepts:
- Import aliases
- Package naming conflicts
- Custom import names
Additional Resources
Go Documentation
Explore the official Go package documentation to see what’s available in the standard library.
A Tour of Go
Take an interactive tour of Go’s features and syntax.
Next Steps
Once you’re comfortable with these basics, move on to:- Variables and Types - Learn about Go’s type system
- Control Flow - Master conditionals and loops