DEV Community

Momchil Atanasov
Momchil Atanasov

Posted on

1

Go’s true can be false

#go

This is something that not all Go developers are probably aware of. Go allows you to do things like the following.

var true = false
Enter fullscreen mode Exit fullscreen mode

Which also means that the following code compiles and runs.

package main

import "fmt"

var true = false

func main() {
  var status bool = true // This will actually be false.
  if status {
    fmt.Println("True")
  } else {
    fmt.Println("False")
  }
}
Enter fullscreen mode Exit fullscreen mode

And it outputs False.

Even code like this is possible.

package main

import "fmt"

var int = false // Wait, what?!?

func main() {
  var status bool = int // Really?!?
  if status {
    fmt.Println("True")
  } else {
    fmt.Println("False")
  }
}
Enter fullscreen mode Exit fullscreen mode

So it seems that some keywords are actually not keywords in Go. I filed an issue a while back and got the reply that this was by design. Not sure what purpose it serves. If you know, please share in the comments.

I wonder if this will be changed in v2, where backward compatibility could be broken.

In the meantime, this is not something to really worry about. Since all these keywords are lowercase, in practice they are private to the package so it's not possible for a dependency to mess your code up.


NOTE: This post is a more condensed version of an old post of mine.

Billboard image

Imagine monitoring that's actually built for developers

Join Vercel, CrowdStrike, and thousands of other teams that trust Checkly to streamline monitor creation and configuration with Monitoring as Code.

Start Monitoring

Top comments (2)

Collapse
 
dogers profile image
Dogers

Go v2 seems less and less likely..
phoronix.com/news/Go-Language-Road...

Collapse
 
mokiat profile image
Momchil Atanasov

Good read, thanks. I guess it makes sense for now, especially since v2 was mostly discussed around generics and now that they managed to introduce them in a backward-compatible way, there is no immediate need for any major changes.

The next big pain point for most developers has to be error handling. It will be interesting to see how the language evolves there.

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay