Go Program Structure
Every Go program follows a predictable structure. Understanding this structure makes it easy to read any Go code, whether written by a beginner or a senior engineer. This topic breaks down all the building blocks that form a complete Go program.
Full Structure at a Glance
// 1. Package Declaration
package main
// 2. Import Statement
import (
"fmt"
"math"
)
// 3. Constants (optional)
const AppName = "MyApp"
// 4. Variables (optional)
var version = "1.0"
// 5. Functions
func greet(name string) string {
return "Hello, " + name
}
// 6. Main Function – Entry Point
func main() {
message := greet("Learner")
fmt.Println(message)
fmt.Println("Version:", version)
fmt.Println("Square root of 16:", math.Sqrt(16))
}
Output:
Hello, Learner
Version: 1.0
Square root of 16: 4
Structure Diagram
┌─────────────────────────────────────┐
│ Go Program Structure │
├─────────────────────────────────────┤
│ 1. package declaration │
│ 2. import statement(s) │
│ 3. constants (optional) │
│ 4. package-level variables (opt.) │
│ 5. helper functions (optional) │
│ 6. func main() ← runs first │
└─────────────────────────────────────┘
1. Package Declaration
The very first line of every Go file declares the package it belongs to.
package main
Packages are Go's way of grouping related code. The package named main is special — it marks the file as an executable program. Files in other packages (like utility code) use other names.
| Package Type | Purpose | Example |
|---|---|---|
main | Executable program | The program entry point |
| Custom name | Reusable library code | package calculator |
2. Import Statements
Imports load packages that contain ready-made code. Go comes with many built-in packages, and more can be added from the internet.
Single Import
import "fmt"
Multiple Imports
import (
"fmt"
"math"
"strings"
)
Go enforces a strict rule: if a package is imported but never used, the program refuses to compile. This keeps the code clean and free of unused dependencies.
3. Constants
Constants hold values that never change throughout the program. They are declared outside functions to make them available everywhere.
const Pi = 3.14159
const AppName = "eStudy247"
4. Package-Level Variables
Variables declared outside any function are available to the entire file.
var counter int = 0
var siteName string = "eStudy247"
5. Functions
Functions are reusable blocks of code. A function has a name, optional parameters, optional return values, and a body.
func add(a int, b int) int {
return a + b
}
Function Structure Diagram
func functionName (parameters) returnType {
│ │ │ │
│ │ │ └── what type it gives back
│ │ └────────────── inputs with types
│ └────────────────────────────── name of the function
└──────────────────────────────────────── keyword to declare a function
body of the function goes here
}
6. The Main Function
The main function is where the program starts running. Go calls it automatically — no need to call it manually anywhere.
func main() {
// code starts executing here
}
Comments in Go
Comments explain what the code does. Go ignores them at runtime.
Single-Line Comment
// This is a single-line comment
fmt.Println("Go is great")
Multi-Line Comment
/*
This is a multi-line comment.
It can span many lines.
*/
fmt.Println("Learning Go")
Code Formatting Rules in Go
Go has strict formatting rules enforced by the gofmt tool. These are the most important ones:
| Rule | Correct | Wrong |
|---|---|---|
| Opening brace on same line | func main() { | func main(){ |
| Tabs for indentation | Tab character | Spaces only |
| No semicolons at end of lines | fmt.Println("hi") | fmt.Println("hi"); |
Run go fmt main.go at any time to automatically fix formatting issues.
Key Points
- Every Go file starts with a package declaration
- Imports bring in reusable code — unused imports cause compile errors
- The
mainfunction is the entry point Go always calls first - Constants and variables can be declared at the package level
- Functions contain reusable logic and can accept inputs and return outputs
- Run
go fmtto keep code formatting consistent
