Go Syntax
A Go source file always has the same three parts, in this order:
- Package declaration —
package main - Imports — the packages the file uses
- Declarations — functions, types, variables and constants
package main // 1. package clause
import "fmt" // 2. imports
// 3. declarations
func main() {
fmt.Println("Package, imports, declarations.")
}Package, imports, declarations.
Statements and semicolons
Go's grammar uses semicolons to end statements, but you almost never type them. The compiler inserts them automatically at the end of a line that ends with an identifier, a literal, ), ], } or keywords like return and break. You only need explicit semicolons to put two statements on one line — and gofmt will usually split such lines anyway.
package main
import "fmt"
func main() {
fmt.Println("one"); fmt.Println("two")
fmt.Println("three")
}one
two
three
The opening brace must stay on the same line
Because of automatic semicolons, Go has one brace style. The { must be on the same line as func, if, for and so on. If you move it to the next line, a semicolon is inserted after main() and the code breaks:
package main
import "fmt"
func main()
{
fmt.Println("This will not compile")
}# command-line-arguments
./example.go:6:1: syntax error: unexpected semicolon or newline before {
Exported and unexported names
Go has no public or private keywords. Instead, the first letter of a name decides its visibility:
| Name starts with | Visible | Example |
|---|---|---|
| Upper-case letter | outside the package (exported) | fmt.Println, strings.ToUpper |
| Lower-case letter | only inside its own package | main, helper |
That is why every function you call from another package — fmt.Println, strings.Repeat — starts with a capital letter.
Identifiers and keywords
Names (identifiers) start with a letter or _ and continue with letters, digits or _. They are case-sensitive: total, Total and TOTAL are three different names. Go style uses camelCase (maxSpeed, userID), not snake_case.
Go has just 25 reserved keywords:
break default func interface select
case defer go map struct
chan else goto package switch
const fallthrough if range type
continue for import return varCode blocks and indentation
Curly braces { } group statements into blocks. Indentation is not meaningful to the compiler, but gofmt indents every block with one tab, so all Go code looks alike.
package main
import "fmt"
func main() {
score := 82
if score >= 50 {
fmt.Println("Passed")
fmt.Println("Score:", score)
}
fmt.Println("Done")
}Passed
Score: 82
Done
Tip: Do not fight
gofmt. Write the code however you like and let the tool format it — every Go editor plugin runs it on save.
Test yourself
Where must the opening { of a function body be placed?
- On the next line
- On the same line as
func - Anywhere
- After a semicolon
Which of these names is exported from its package?
parseLine_ParseParseLineparse_line
What is the correct order of a Go file?
- imports, package, functions
- package, imports, declarations
- functions, package, imports
- Any order works