HowtoGo
Home / Basics / Keywords
Basics

Keywords

The 25 reserved words that make up Go's grammar — declarations, control flow, types, and concurrency primitives. Unchanged since Go 1.0 and still the full set through Go 1.26.

break

Terminates the innermost for, switch, or select statement.

case

Introduces a clause in a switch or select statement.

chan

Declares a channel type used for goroutine communication.

const

Declares a constant value, fixed at compile time.

continue

Skips to the next iteration of the innermost for loop.

default

Specifies the default clause in a switch or select statement.

defer

Schedules a function call to run after the surrounding function returns.

else

Introduces the alternative branch of an if statement.

fallthrough

Transfers control to the next case clause in a switch statement.

for

Introduces a loop; Go's only looping construct.

func

Declares a function, method, or function literal.

go

Starts a new goroutine running a function call.

goto

Transfers control to a labeled statement in the same function.

if

Introduces a conditional statement.

import

Declares packages whose exported identifiers may be used in the file.

interface

Declares an interface type, a set of method signatures.

map

Declares a map type, an unordered collection of key-value pairs.

package

Declares the package to which the current source file belongs.

range

Iterates over elements of an array, slice, string, map, channel, or integer.

return

Terminates a function and optionally returns values to the caller.

select

Waits on multiple channel communication operations.

struct

Declares a struct type, a sequence of named fields.

switch

Introduces a multi-way conditional statement.

type

Declares a new named type or type alias.

var

Declares one or more variables.

Examples

var, const, type, func, import, and package are the vocabulary of top-level and local declarations. Only package and import are file-scoped; the rest can appear almost anywhere.

package main

import "fmt"

const MaxRetries = 3

type Job struct {
    ID    int
    Tries int
}

func retry(j Job) bool {
    var ok bool = j.Tries < MaxRetries
    return ok
}

fmt.Println(retry(Job{ID: 1, Tries: 2}))
Output
true

In practice

range combined with continue skips completed tasks without an extra if/else branch. The named zero Task return relies on var producing the type's zero value when nothing is found.

type Task struct {
    Name string
    Done bool
}

func nextPending(tasks []Task) (Task, bool) {
    for _, t := range tasks {
        if t.Done {
            continue
        }
        return t, true
    }
    var zero Task
    return zero, false
}

A switch with an init statement scopes t and ok to the switch itself, the same pattern used for if with an init clause.

tasks := []Task{
    {Name: "design", Done: true},
    {Name: "build", Done: false},
    {Name: "ship", Done: false},
}

switch t, ok := nextPending(tasks); {
case !ok:
    fmt.Println("nothing pending")
default:
    fmt.Println("next up:", t.Name)
}
Terminal
$ go run main.go
next up: build