6
  1. What version of Go are you using (go version)? https://play.golang.org

  2. What did you do?

Run a small program:

package main

import "fmt"

const true = false

func main() {
    if (true == false) {
        fmt.Println("True equals to false")
    }
    fmt.Println("Hello World")
}

https://play.golang.org/p/KwePsmQ_q9

  1. What did you expect to see?

Error or warning message that I'm creating constant with already defined name, and potentially breaking whole app.

  1. What did you see instead?

Running without a problem. No warnings or anything to prevent creating new constant with already defined name.

Zhomart
  • 702
  • 1
  • 7
  • 19
  • 1
    Funny thing. "Var true = false" gives the same result. But when I try to declare a package or import I get this error: "prog.go:6: syntax error: unexpected package, expecting name prog.go:6: cannot declare name " – Vityata Mar 23 '16 at 08:42
  • 2
    Possible duplicate of [Why are 'new' and 'make' not reserved keywords?](http://stackoverflow.com/questions/31987772/why-are-new-and-make-not-reserved-keywords) It asks about `new` and `make`, but `true` is also a [predeclared identifier](https://golang.org/ref/spec#Predeclared_identifiers) which makes the answers apply here too. – icza Mar 23 '16 at 08:48
  • 2
    Neither true nor false are keywords (https://golang.org/ref/spec#Keywords) but predeclared identifiers (https://golang.org/ref/spec#Predeclared_identifiers) and you redefine them which is fine and works like the whole language. – Volker Mar 23 '16 at 08:49

1 Answers1

10

true and false are not reserved keywords. These are predeclared identifiers.

const (
        true  = 0 == 0 // Untyped bool.
        false = 0 != 0 // Untyped bool.
)

This means that true and false are simple two untyped boolean values. This is the reason that in your example true is equal to false.

https://golang.org/pkg/builtin/#pkg-constants

Endre Simo
  • 11,330
  • 2
  • 40
  • 49