📐 Go Program Structure – Get Started with Your First Go Program
🧲 Introduction – How Go Code is Organized
Every Go program, no matter how simple or complex, follows a consistent structure that ensures readability, maintainability, and easy compilation. Understanding the Go program structure is the first step toward writing clean, idiomatic Go code.
🎯 In this section, you’ll learn:
- The layout of a typical Go program
- Mandatory components like
package mainandfunc main() - How to use packages and comments effectively
- How to compile and execute a Go file
🔧 Basic Go Program Layout
Let’s look at the classic “Hello, World!” program in Go:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
🧩 Explanation of Each Component
| Part | Description |
|---|---|
package main | Declares this file as part of the main package (needed to compile and run as an executable). |
import "fmt" | Brings in the fmt package from Go’s standard library, used for formatted I/O. |
func main() | Entry point of the program. The Go runtime executes this function first. |
fmt.Println(...) | Prints output to the console. In this case, it prints “Hello, World!” |
🗂️ File Extension and Naming
- Every Go source file must have the
.goextension. - The file name can be anything, but the
mainpackage must have amain()function. - Go programs are case-sensitive.
🧪 Compiling & Running a Go Program
Once you’ve written your code in hello.go, follow these steps:
# Run without compiling permanently
go run hello.go
Output:
Hello, World!
To compile it into a binary:
go build hello.go
This creates an executable:
helloon Linux/macOShello.exeon Windows
Run it:
./hello
✍️ Adding Comments in Go
Go supports two styles of comments:
// Single-line comment
/*
Multi-line comment
that spans multiple lines
*/
Use comments to explain logic, disable code, or document sections. Go also uses comments for documentation generation via godoc.
🔍 More About the main Function
- Go does not allow overloading, so there’s only one
main()function. - Any file in
package mainthat containsfunc main()can act as the entry point. - A Go program won’t compile without this setup for executables.
✅ Example: Command-Line Arguments
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <yourname>")
return
}
name := os.Args[1]
fmt.Println("Hello,", name)
}
Run:
go run main.go John
Output:
Hello, John
🧠 os.Args[0] is always the executable name. User arguments start from index 1.
📌 Summary – Recap & Next Steps
You’ve now learned the foundational structure of a Go program, from package declarations to running executables. This knowledge forms the backbone of writing real-world Go applications.
🔍 Key Takeaways:
- Every Go program starts with
package mainandfunc main() - Use the
importkeyword to access libraries - Use
go runorgo buildto execute your code - Comments in Go are simple and used for documentation and clarity
⚙️ Next: Learn about Go’s basic syntax, variable declarations, and type system.
❓ FAQs – Go Program Structure
❓ Why do I need package main?
✅ It defines the program as an executable entry point. Without it, Go will compile the file as a library, not a runnable program.
❓ Can I have multiple main() functions?
✅ No. Only one main() is allowed per main package. However, you can have multiple Go files under the same package.
❓ What is the use of import in Go?
✅ It allows you to include other packages (standard or custom) to access additional functions and types.
❓ How do I print in Go?
✅ Use the fmt package’s Println, Printf, or Print functions.
❓ What if I don’t include a main() function?
✅ Your code will compile as a package, but it won’t run. main() is mandatory in any Go executable.
Share Now :
