Beginner Fundamentals
Syntax
Go has a clean and consistent syntax. Every source file follows the same basic structure, which makes Go code easy to read across projects.
Package declaration
The first line of every file declares its package. An executable program uses main.
package main
Imports
After the package line, you import the packages you need.
import "fmt"
The main function
Execution of an executable starts in the main function. Curly braces are required and the opening brace must be on the same line.
package main
import "fmt"
func main() {
fmt.Println("Structure matters")
}
Implicit semicolons
Go statements end with a semicolon, but the compiler inserts them automatically at line ends. So you write one statement per line without typing semicolons.
x := 10
y := 20
fmt.Println(x + y)
Because of this rule, the brace style is fixed: func main() { must not have the brace on the next line.