4 ms·
> 'type Foo int' in Go creates a new 'Foo' type that can't be used as an int without casting (although you can assign an int to it). You can't, in fact: t
by paskozdilar 4y ago
> 'type Foo int' in Go creates a new 'Foo' type that can't be used as an int without casting (although you can assign an int to it).
You can't, in fact:
type Foo int
func main() {
var foo Foo = 1
var bar int = 2
foo = bar // cannot use bar (variable of type int) as Foo value in assignment
}
The reason you can do things like:
var foo Foo = 1
is that because numeric literals in Go are not ints, they are untyped [0].
[0] https://go.dev/blog/constants https://go.dev/blog/constants
- foldr 4y agoAh yes, good point. Thanks for the correction.