In C and C++[1][2] "int" and "float" are reserved keywords and thus can't be used as identifiers. In Go[3], "int" and "float64" are "predeclared identifiers"[4] and can be redefined, just like any other identifier.
[1] - https://en.cppreference.com/w/c/keyword [2] - https://en.cppreference.com/w/cpp/keyword [3] - https://go.dev/ref/spec#Keywords [4] - https://go.dev/ref/spec#Predeclared_identifiers On Friday, February 11, 2022 at 1:20:37 PM UTC-5 kziem...@gmail.com wrote: > Hello, > > I have so background in C and C++ so was used to that you CAN'T define > variable with the name of predefined types. But, in Go 1.17 (go version > go1.17.7 linux/amd64) > > package main > > > > import "fmt" > > > > func main() { > > var int float64 = 2.5 > > > > fmt.Printf("int: %v, %T\n", int, int) > > > > // var varOne int = 1 > > > > // fmt.Printf("varOne: %v", varOne) > > } > this code compile and produce result > > int: 2.5, float64 > > When you uncomment two lines in this example you will get error > > int is not a type > > In Go 1.18beta2 (go version go1.18beta2 linux/amd64) > > package main > > > > import ( > > "fmt" > > // "math" > > ) > > > > type Constraint interface { > > float64 > > } > > > > func someFunction[uint8 Constraint](x uint8) uint8 { > > x += uint8(1) > > return x > > } > > > > func main() { > > var x float64 = 1 > > > > fmt.Printf("someFunction(%v): %v\n", x, someFunction(x)) > > fmt.Printf("x type: %T\n", x) > > fmt.Printf("someFunction(x) type: %T\n", someFunction(x)) > > } > this code compile and produce result > > someFunction(1): 2 > > x type: float64 > > someFunction(x) type: float64 > > Can anyone tell me why this behavior is possible in Go? In the past I > never write "var int int" because it is wicked and possible sinful, but > syntax for generic functions make easier for me to produce such evil things. > > This is a bit too much shocking things for me for one day, I will be back > tomorrow. > > Best, > Kamil -- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/78714732-cd31-49cf-a941-2ed228eb2b7fn%40googlegroups.com.