July 7, 2026

Go Basic Syntax – Structure of a Go Program

Updated July 2, 2026. Refreshed for Go 1.22+ and current SEO best practices.

Go 1.22+ Notes

Run go fmt ./... after editing any lesson code. Go 1.22+ uses the same basic syntax shown here — packages, func main, and the standard library remain the foundation for backend and cloud-native projects.

In this lesson we cover Go basic syntax — the building blocks every Go program shares. If you completed Go Programming – Setup, you already ran your first program. Here we explain why that program looks the way it does and the rules you must follow when writing Go code.

Prerequisites: Go 1.22 or newer installed (go version in your terminal). Estimated time: 30–40 minutes.

1. Structure of a Go Program

Every executable Go program has the same skeleton: a package declaration, import statements, and a main function. Consider this program (the same one from the Setup lesson):

package main

import "fmt"

func main() {
	fmt.Println("Welcome to Go!")
}

Let us walk through it line by line:

  • package main — Declares that this file belongs to the main package. Programs that compile to an executable must use package main.
  • import "fmt" — Brings in the fmt (formatting) standard library so we can print to the console.
  • func main() — The entry point. Execution starts here when you run the program.
  • fmt.Println(...) — Calls the Println function from fmt and prints a line of text.

Go source files use the .go extension. A small project might live in a single file; larger projects split code across many files in the same package or across multiple packages inside a module (created with go mod init).

2. Comments in Go

Comments are notes for humans — the compiler ignores them. Go supports two styles:

// This is a single-line comment

/*
   This is a
   multi-line comment
*/

package main

import "fmt"

func main() {
	// Print a greeting
	fmt.Println("Hello, Kindson The Genius!")
}

Use comments to document your name, the date, and what the program does — especially in learning exercises.

3. Identifiers and Naming Rules

An identifier is a name you give to a variable, function, type, or package. In Go, identifiers must:

  • Start with a letter or underscore (_)
  • Contain only letters, digits, and underscores after the first character
  • Not use special characters such as $, @, or spaces

Exported vs unexported: If an identifier starts with an uppercase letter, it is exported (visible outside its package). Lowercase names are private to the package. For example, Println is exported from fmt; an unexported helper might be named println.

Valid identifiers: name, userID, _temp, MaxRetries
Invalid identifiers: 2fast, user-name, go!

4. Go Reserved Words (Keywords)

Keywords are reserved by the language — you cannot use them as identifier names. Go has 25 keywords:

Keywords Keywords Keywords
break default func
interface select case
defer go struct
chan else goto
map switch const
fallthrough if range
type continue for
import package var

Notice keywords like go, defer, select, and range — they support Go’s concurrency and iteration features you will meet in later lessons.

5. Code Blocks and Braces

Unlike Python, Go uses curly braces { } to define blocks of code — not indentation alone. However, consistent indentation (tabs are standard) makes code readable.

func main() {
	if true {
		fmt.Println("Inside the if block")
	}
}

Every opening brace { must have a matching closing brace }. Go automatically inserts semicolons at the end of lines during compilation, so you rarely type ; yourself — but statements are still logically terminated.

6. Whitespace and Formatting with gofmt

Go treats extra spaces, blank lines, and tabs mostly as separators — but style matters for readability. The Go toolchain includes gofmt (or go fmt), which formats code to a community standard:

go fmt main.go
# or format all files in the current directory:
go fmt ./...

Most editors — including VS Code with the Go extension and GoLand — run gofmt on save. This is why Go code across projects looks consistent.

7. Running Your Program

From the Setup lesson you ran code inside GoLand. From the terminal, inside your project folder:

# Compile and run in one step (great while learning)
go run .

# Build an executable binary
go build -o myapp .
./myapp

go run is ideal for quick experiments. go build produces a standalone binary you can deploy — one reason Go is popular for servers and DevOps tools.

8. Next Steps

You now understand Go basic syntax: packages, imports, func main, comments, identifiers, keywords, and blocks. In the next lesson we cover variables and data types — how to store and work with values in Go.

Continue the series: Lesson 1 – Introduction · Lesson 2 – Setup

Frequently Asked Questions

What is the basic structure of a Go program?
A Go program starts with package main, imports the packages it needs, and defines func main() where execution begins.

Does Go require semicolons?
Go inserts semicolons automatically at line endings during compilation. You almost never write them manually.

What is the difference between go run and go build?
go run compiles and executes immediately without leaving a binary file. go build creates an executable you can run or ship separately.

Want live Go or backend classes? Join Alkademy for instructor-led programming courses with hands-on projects.


0 0 votes
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
trackback

[…] Previous: Lesson 3: Go Basic Syntax – Structure of a Go Program […]

1
0
Would love your thoughts, please comment.x
()
x