Tuesday, December 8, 2020

Go Reserved Keywords

Go is not a big language but would you be able to list all of its reserved keywords?

On a recent post we reviewed the main keywords on a Go program. On that article we discussed that some of the reasons to use Go are that it's easy to learn and scales so well is due to its simplicity. In fact, the language is so small that it may be very simple for a new user of the language to get accustomed to it very quickly.

But that's not a coincidence. The language was built with simplicity as a core value meaning that every modification in the language (especially those increasing it) is extensively debated by the community.

Go Keywords

Go has 25 reserved keywords 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

Note that because the above keywords are reserved, you cannot name your variables using one of those words.

Predeclared names

In addition to the predeclared keywords, these are the predeclared names you'll see in Go:

  • 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

Predeclared names are nor reserved by the language, meaning you're free to use them (with moderation) in your code whenever it makes sense. For example, this code would work:

func main() {
len := 15
fmt.Println("Len:", len)

While this wouldn't:

func main() {
goto := 15
fmt.Println("Goto:", goto)

./prog.go:8:7: syntax error: unexpected :=, expecting name
./prog.go:9:22:syntax error: unexpected goto, expecting expression


On this post we learned about Go's reserved keywords. If you're new to Go but not to programming, you definitely would be able identify most of them!

When developing in Go it's important to understand its reserved keywords as you will be constantly interacting with them. While will discuss more each of the above keywords in future posts, we also recommend you to understand well how variables and zero values work in Go.


Any comment about this page? Please contact us on Twitter

Featured Article

Pointers in Go

Understand all about declaring and using pointers in Go If you're coming from Python, Java, JavaScript, C# and others, tal...

Popular Posts