TExcelTAD
Home › Go › Go Syntax

Go Syntax

A Go source file always has the same three parts, in this order:

  1. Package declaration — package main
  2. Imports — the packages the file uses
  3. Declarations — functions, types, variables and constants
go · try it yourself
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.

go · try it yourself
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:

go · try it yourself
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:

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

Code 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.

go · try it yourself
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?

  1. On the next line
  2. On the same line as func
  3. Anywhere
  4. After a semicolon

Which of these names is exported from its package?

  1. parseLine
  2. _Parse
  3. ParseLine
  4. parse_line

What is the correct order of a Go file?

  1. imports, package, functions
  2. package, imports, declarations
  3. functions, package, imports
  4. Any order works