Skip to content

Go Basic Syntax

This chapter introduces the fundamental building blocks of a Go program.

A Go program is composed of tokens. A token can be a keyword, an identifier, a constant, a string literal, or a symbol. Consider the following Go statement:

package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}

This program uses several tokens:

Keywords: package, import, func
Identifiers: main, fmt, Println
String Literal: "Hello, World!"
Symbols: ., (, )

In Go, the newline character generally acts as a statement terminator. Unlike languages like C or Java, you usually don’t need to place semicolons (;) at the end of each statement. The Go compiler automatically inserts semicolons based on lexical rules.

For example, these are two separate statements:

fmt.Println("Hello, World!")
fmt.Println("Learning Go syntax!")

However, if you want to write multiple statements on a single line, you must separate them with semicolons:

fmt.Print("First"); fmt.Print("Second") // Semicolon required here

Comments are explanatory text ignored by the compiler. Go supports two types of comments:

// This is a single-line comment
var message = "Hello" // Comment after code
/*
This is a multi-line comment.
It can span several lines.
*/

Comments cannot be nested. They also don’t affect the interpretation of string or character literals.

Tip: Use comments prefixed with the name of the declared item (e.g., // User represents a user or /* Package config handles configuration */) right before package, type, function, or variable declarations to generate documentation using the go doc tool. These are called doc comments.

An identifier is a name used to identify variables, functions, types, constants, etc. Rules for identifiers:

Examples of valid identifiers:

userService _tempVar isValid MAX_RETRIES piValue

Visibility (Exported vs. Unexported): A crucial convention in Go is that identifiers starting with an uppercase letter are exported (publicly accessible from other packages). Identifiers starting with a lowercase letter or underscore are unexported (private to the package where they are defined).

var InternalCounter int // Unexported (private to this package)
const ExportedConstant = 3.14 // Exported (public)

Keywords are reserved words with special meaning in the Go language. They cannot be used as identifiers. Here is the list of Go keywords:

breakdefaultfuncinterfaceselect
casedefergomapstruct
chanelsegotopackageswitch
constfallthroughifrangetype
continueforimportreturnvar

Whitespace refers to spaces, tabs, and newline characters. Go uses whitespace to separate tokens. Blank lines (containing only whitespace and possibly comments) are ignored by the compiler.

In this statement, whitespace is necessary between var, age, and int:

var age int

In this statement, whitespace around = and + is optional but improves readability:

total = items + tax // Calculate the total

Go Best Practice: Use the gofmt tool (or go fmt) to automatically format your code according to standard Go conventions. Most Go development environments integrate this tool, ensuring consistent whitespace and style.