Introduction
A .go file holds source code in Go, the
compiled language created at Google. The go command builds all the
.go files in a folder together as one package, and a go.mod
file at the top of a project names the module and the versions of its
dependencies.
Files whose names end in _test.go hold tests and are only compiled by
go test.
The pages listed below are a reference to the language and to the .go
format. They follow The Go Programming Language
Specification, language version go1.27 (26 May 2026), and name the specification
section for each topic. Go 1.27 is the current release; the
Versions page lists the language changes in every
release.
Structure
A Go source file is plain UTF-8 text with a package clause, then
import declarations, then functions, types, constants and variables in any
order. Every file in a folder belongs to one package. See
File structure for the encoding, file names,
build constraints and directives, and
Lexical structure for the tokens.
package main
import "fmt"
func main() {
fmt.Println("Hello, world")
}
Language reference
The reference is split into these pages, in roughly the order of the specification.
- File structure – source file organisation, the package clause, imports, UTF-8 and the byte order mark, build constraints,
_test.goand file-name suffixes, the cgo preamble,go:embedand other directives,gofmt - Lexical structure – tokens, automatic semicolons, comments, identifiers, keywords, operators and every kind of literal
- Types – every kind of type, method sets, identity, assignability, zero values and conversions
- Declarations and scope – blocks, scope,
constandiota,var, short declarations, type declarations, aliases and predeclared identifiers - Expressions – the full operator table with precedence, composite literals, selectors, slices, type assertions, calls and order of evaluation
- Statements – every statement, labels, and all forms of
range - Functions and methods – variadics, closures,
defer,panicandrecover, receivers, method values and expressions - Interfaces – type sets, embedding, type elements and nil interface values
- Generics – type parameters, constraints, inference, instantiation and generic methods
- Packages and modules – initialisation order,
internal,go.modand module paths, vendoring and workspaces - Concurrency – goroutines, channels,
selectand the memory model - Built-ins – the predeclared functions and package
unsafe - Standard library – the package groups, briefly
- Versions – Go 1.0 in 2012 to Go 1.27 in 2026, with the language changes in each
Not described
Known, not yet covered
- Reference: the standard library page groups the packages briefly. It does not describe the types, functions and methods inside any package (
fmt,net/http,syncand the rest); pkg.go.dev does. - Reference: the
gocommand (build,test,vet,mod), the testing and benchmarking conventions beyond the_test.gofile name, the compiler and linker flags, and Go assembly (the Plan 9 style.sfiles that sit beside Go files) are not described.go.modhas its own overview. - Viewer: only the built-in types are coloured as types (
int,string,error,any,comparableand the like). A type you declare (type S struct), a type parameter such asT, and a type from another package are plain, and the predeclared functions (len,make,min) are coloured like any function call. - Viewer: comments that begin
//go:are marked as directives, but other tool comments (//export,//line,// +build) are ordinary comments, and the C code in a cgo preamble is a comment, not scanned as C. - Viewer: the file is not type-checked or compiled: there is no outline or list of problems, unused imports, unused variables and missing packages are not reported, and
importpaths and build constraints are not followed or evaluated. Only the first 1 MiB of a file is read and coloured.
Links
Software
- Go – the official toolchain, with the
gocommand andgofmt(Windows, macOS, Linux) - Visual Studio Code – free code editor with a Go extension (Windows, macOS, Linux)
- GoLand – JetBrains’ development environment for Go (Windows, macOS, Linux)
- Vim – text editor with Go syntax support (Windows, macOS, Linux)
Developers
- go/parser – Go’s own package for parsing Go source (Go)
- go/ast – Go’s own package for the syntax tree that go/parser builds (Go)
- tree-sitter-go – Go grammar for the Tree-sitter parser used by editors
Reference
- The Go Programming Language Specification – the official description of the language