Go Keywords and Identifiers
Keywords are predefined words, and these are the reserved words and hold a special meaning.
Identifiers are user-defined words. It is nothing but the name given to the entities such as variables, constants, classes, and functions.
When playing with definitions of variables and constants, you may have noticed that the compiler does not allow you to use certain names for variables. For example, the following code will not compile:
package main
func main() {
type := "How are you?"
println(type)
}
Let us explain what is going on here.
Keywords
A keyword is a word that is reserved by the language and cannot be used for any other purpose. For example, true
and package
are keywords in Go. The full list of keywords is available in the Go specification.
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
You may have recognized some of these keywords (such as var
, const
, func
and package
) from the previous tutorials. By the end of this course, you will have seen all of them.
Identifiers
An identifier is a name that is used to identify a variable, function, type, etc.
Predeclared identifiers
Some of them are predefined (such as true
, false
and nil
), and you should not use them for your own purposes (unless you have a perfect reason to do so).
Here is the complete list for reference:
1) Types:
any | bool | byte | comparable |
complex64 | complex128 | error | float32 |
float64 | int | int8 | int16 |
int32 | int64 | rune | string |
uint | uint8 | uint16 | uint32 |
uint64 | uintptr | ||
2) Constants:
true
, false
, ota
3) Zero value:
nil
4) Functions:
append | cap | close | complex |
copy | delete | imag | len |
make | new | panic | print |
println | real | recover | |
User-defined identifiers
As long as you don't use any word that appears in one of the lists above, and as long as you follow those two simple rules, you can choose any name you want for your variables, constants, functions, etc.
- The first character must be a letter.
- The remaining characters can be letters or digits.
Note: The underscore character (_
) is considered a letter for the purpose of this rule.
Consequently, the following names are valid identifiers:
message
,myName
,YOUR_NAME
,myName42
.
But it is considered a good style to use mixedCase
for variables, constants, and functions rather than snake_case
(as in Python).