Table of Contents
- Numeric types
intand vice versa
- Type Alias
- Conversions(Type casting)
Regardless of their size,
uintptrare different types from their explicitly sized siblings. Thus
intis not the same type as
int32, even if the natural size of integers is 32 bits, and an explicit conversion is required to use an
intvalue where an
int32is needed, and vice versa.
For this reason,
Although Go provides unsigned numbers and arithmetic, we tend to use the signed int form even for quantities that can’t be negative, such as the length of an array, though uint might seem a more obvious choice.
Unsigned numbers tend to be used only when their bitwise operators or peculiar arithmetic operators are required,
The conversion operation
int(x), converts the value
Tif the conversion is allowed. Many integer-to-integer conversions do not entail any change in value; they just tell the compiler how to interpret a value. But a conversion that narrows a big integer into a smaller one, or a conversion from integer to floating-point or vice versa, may change the value or lose precision.
- http://www.gopl.io/ (3.1)
Numeric types reference
the set of all unsigned 8-bit integers (0 to 255)
the set of all unsigned 16-bit integers (0 to 65535)
the set of all unsigned 32-bit integers (0 to 4294967295)
the set of all unsigned 64-bit integers (0 to 18446744073709551615)
the set of all signed 8-bit integers (-128 to 127)
the set of all signed 16-bit integers (-32768 to 32767)
the set of all signed 32-bit integers (-2147483648 to 2147483647)
the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807)
the set of all IEEE-754 32-bit floating-point numbers
the set of all IEEE-754 64-bit floating-point numbers
the set of all complex numbers with float32 real and imaginary parts
the set of all complex numbers with float64 real and imaginary parts
- Implementation specific
either 32 or 64 bits
same size as
an unsigned integer large enough to store the uninterpreted bits of a pointer value
int and vice versa howto
runeis internally an alias of
int32, we can just convert one into another.
xis assignable to a variable of type
x's type is identical to
Thave identical underlying types and at least one of
Tis not a defined type.
Tis an interface type and
xis a bidirectional channel value,
Tis a channel type,
Thave identical element types, and at least one of
Tis not a defined type.
xis the predeclared identifier
Tis a pointer, function, slice, map, channel, or interface type.
xis an untyped constant representable by a value of type
Type Alias discussion
From 1.9, Go started to support type aliases. Consider following:
While type declaration creates a different type from an underlying type, type alias creates just a name for the original type.
- While a value of
Name1is assignable to
map[string]string, is not to
Aliasis assignable to any of those types
- As a method receiver should be a defined type, a type alias of primitive types