How many keywords are there in Go

Created
Modified

Keywords

The following keywords are reserved and may not be used as identifiers.

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

They can be categorized as four groups:

Declaration

const, func, import, package, type and var are used to declare all kinds of code elements in Go programs.

const
The ‘const’ keyword is used to introduce a name for a scalar value like 2 or 3.14, etc.
func
The ‘func’ keyword is used to declare a function.
import
The ‘import’ keyword is used to import packages.
package
The code is grouped as a unit in a package. The ‘package’ keyword is used to define one.
type
We can use the ‘type’ keyword to introduce a new struct type.
var
The ‘var’ keyword is used to create the variables in the ‘Go’ language.

Control Flow

chan, interface, map and struct are used as parts in some composite type denotations.

chan
The ‘chan’ keyword is used to define a channel. In ‘Go’, you are allowed to run parallel pieces of code simultaneously.
interface
The ‘interface’ keyword is used to specify a method set. A method set is a list of methods for a type.
map
The ‘map’ keyword defines a map type. A map os an unordered collection of the key-value pairs.
struct
Struct is a collection of fields. We can use the ‘struct’ keyword followed by the field declarations.

Composite Types

break, case, continue, default, else, fallthrough, for, goto, if, range, return, select and switch are used to control flow of code.

break
The ‘break’ keyword lets you terminate a loop and continue execution of the rest of the code.
case
This is a form of a ‘switch’ construct. We specify a variable after the switch.
continue
The ‘continue’ keyword allows you to go back to the beginning of the ‘for’ loop.
default
The ‘default’ statement is optional but you need to use either case or default within the switch statement.
else
The ‘else’ keyword is used with the ‘if’ keyword to execute an alternate statement if a certain condition is false.
fallthrough
This keyword is used in a case within a switch statement. When we use this keyword, the next case is entered.
for
The ‘for’ keyword is used to begin a for loop.
goto
The ‘goto’ keyword offers a jump to a labeled statement without any condition.
if
The ‘if’ statement is used to check a certain condition within a loop.
range
The ‘range’ keyword lets you iterate items over the list items like a map or an array.
return
Go allows you to use the return values as variables and you can use the ‘return’ keyword for that purpose.
select
The ‘select’ keyword lets a goroutine to wait during the simultaneous communication operations.
switch
The ‘switch’ statement is used to start a loop and use the if-else logic within the block.

Function Modifier

defer and go are also control flow keywords, but in other specific manners.

defer
The ‘defer’ keyword is used to defer the execution of a function until the surrounding function executes.
go
The ‘go’ keyword triggers a goroutine which is managed by the golang-runtime.

Predeclared Names

In addition, there are about three dozen predeclared names like int and true for built-in con- stants, types, and functions.

Constants
true false iota nil
Types
int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex128 complex64 bool byte rune string error
Functions
make len cap new append copy close delete complex real imag panic recover

Related Tags