Home »
Golang
Golang Keywords
By IncludeHelp Last updated : December 8, 2023
What are keywords in Go Language?
Keywords are reserved words whose meanings are pre-defined in the compiler and we cannot change their meaning. Keywords can also not be used for any other purpose like using them as identifiers. If we use keywords as the identifier, this will generate an error.
List of Golang keywords
There are 25 keywords in Golang, they are:
break | case | chan | const | continue |
default | defer | else | fallthrough | for |
func | go | goto | if | import |
interface | map | package | range | return |
select | struct | switch | type | var |
Example of Golang Keywords
Consider the below program,
package main
import (
"fmt"
)
func main() {
var name = "Alex"
fmt.Println("Hey! My name is", name)
}
Output
Hey! My name is Alex
In the above program, the keywords are: package, import, func, and var.
What happens if we use a keyword as an identifier?
If we use a keyword as an identifier in Golang (and, other programming languages also), the program troughs an error.
package main
import (
"fmt"
)
func main() {
// Here, we are using keyword "range"
// as an identifier (i.e., variable name)
var range = "Alex"
fmt.Println("Hey! My name is", range)
}
Output
./prog.go:10:6: syntax error: unexpected range, expecting name
./prog.go:11:33: syntax error: unexpected range, expecting expression
In the program, we are using the keyword range as a variable name to store a string and the result is a syntax error.