Package and Main
package main
func main() {
}The entry point of a Go program.
Go syntax quick reference: slices, maps, structs, errors, goroutines and channels, from the Go specification. Every item below is clearly marked as a shortcut, command, or syntax reference.
package main
func main() {
}The entry point of a Go program.
fmt.Println("Hello")
fmt.Printf("%d\n", n)Prints a formatted line.
var n int = 1
name := "Ada"Declares with var or the short := form.
if err != nil {
return err
}Checks a returned error.
import (
"fmt"
"os"
)Imports standard packages.
if err := run(); err != nil {
...
}Runs a statement before the condition.
for i := 0; i < 10; i++ {
}Go's only loop keyword.
for i, v := range items {
}Loops over a slice or map.
The entry point of a Go program.
package main
func main() {
}Prints a formatted line.
fmt.Println("Hello")
fmt.Printf("%d\n", n)Declares with var or the short := form.
var n int = 1
name := "Ada"Checks a returned error.
if err != nil {
return err
}Imports standard packages.
import (
"fmt"
"os"
)Runs a statement before the condition.
if err := run(); err != nil {
...
}Go's only loop keyword.
for i := 0; i < 10; i++ {
}Loops over a slice or map.
for i, v := range items {
}Creates a resizable list.
nums := []int{1, 2, 3}
nums = append(nums, 4)Creates key-value pairs.
ages := map[string]int{"Ada": 36}Defines a function with multiple returns.
func div(a, b int) (int, error) {
...
}Groups related fields.
type User struct {
Name string
Age int
}Attaches a method to a type.
func (u *User) Greet() string {
return "Hi " + u.Name
}Runs a function concurrently.
go work()Runs or builds Go code.
go run .
go buildDeclares a constant.
const Max = 100Branches on a value (no break needed).
switch n {
case 1:
...
default:
...
}Checks whether a key exists.
v, ok := ages["Bob"]Describes behavior a type must have.
type Shape interface {
Area() float64
}Sends and receives between goroutines.
ch := make(chan int)
ch <- 1
v := <-chRuns a call when the function returns.
defer f.Close()It covers 21 common Go patterns and examples, including Package and Main, Print Output, Declare a Variable, Handle an Error, Import Packages.
Yes. Each reference page includes a copyable code example.
Check the linked documentation for language or runtime version requirements. Libraries, file paths, terminal quoting and environment settings can differ across systems; no blanket platform compatibility is implied.