DEV Community

BlazingBits
BlazingBits

Posted on

Go's list of keywords is surprisingly short

Every language has a list of reserved keywords you aren't allowed to use, but Go's list is surprisingly short leaving it open to do all kinds of funky things.

Below is a list of all the reserved keywords in Go (as of writing this post):

  • 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

( Source )

An Experiment

The keen eyed among you might have noticed that there are two missing values on that list that one might expect to be there.

Notice the words true and false aren't listed as reserved keywords in Go. Does this mean we can reassign the meaning of true and false?

Lets perform a small experiment to find out:

package main

import "fmt"

func main() {
  false := true
  fmt.Printf("In Boolean Logic, is a false statement actually true? %v", false == true)
}

Enter fullscreen mode Exit fullscreen mode

Running this short experiment produces the output:

In Boolean Logic, is a false statement actually true? true
Enter fullscreen mode Exit fullscreen mode

There You Have It

As you can see by our short experiment above, true and false not being reserved by the Go language lets you turn boolean logic upside down. Stop means go. No means Yes. Up means down. Its a quiet a powerful trick to have and I would definitely NOT recommend using this in any real code.

What other words did you notice weren't reserved? Can you think of any other universe altering techniques you could get away with?

Let me know in the comments (:

Top comments (0)