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.
constThe ‘const’ keyword is used to introduce a name for a scalar value like 2 or 3.14, etc.funcThe ‘func’ keyword is used to declare a function.importThe ‘import’ keyword is used to import packages.packageThe code is grouped as a unit in a package. The ‘package’ keyword is used to define one.typeWe can use the ‘type’ keyword to introduce a new struct type.varThe ‘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.
chanThe ‘chan’ keyword is used to define a channel. In ‘Go’, you are allowed to run parallel pieces of code simultaneously.interfaceThe ‘interface’ keyword is used to specify a method set. A method set is a list of methods for a type.mapThe ‘map’ keyword defines a map type. A map os an unordered collection of the key-value pairs.structStruct 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.
breakThe ‘break’ keyword lets you terminate a loop and continue execution of the rest of the code.caseThis is a form of a ‘switch’ construct. We specify a variable after the switch.continueThe ‘continue’ keyword allows you to go back to the beginning of the ‘for’ loop.defaultThe ‘default’ statement is optional but you need to use either case or default within the switch statement.elseThe ‘else’ keyword is used with the ‘if’ keyword to execute an alternate statement if a certain condition is false.fallthroughThis keyword is used in a case within a switch statement. When we use this keyword, the next case is entered.forThe ‘for’ keyword is used to begin a for loop.gotoThe ‘goto’ keyword offers a jump to a labeled statement without any condition.ifThe ‘if’ statement is used to check a certain condition within a loop.rangeThe ‘range’ keyword lets you iterate items over the list items like a map or an array.returnGo allows you to use the return values as variables and you can use the ‘return’ keyword for that purpose.selectThe ‘select’ keyword lets a goroutine to wait during the simultaneous communication operations.switchThe ‘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.
deferThe ‘defer’ keyword is used to defer the execution of a function until the surrounding function executes.goThe ‘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.
Constantstrue false iota nilTypesint int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 uintptr float32 float64 complex128 complex64 bool byte rune string errorFunctionsmake len cap new append copy close delete complex real imag panic recover