Types
Table of Contents
- Integers
- Numeric types
- Covert
runetointand vice versa - Assignability
- Type Alias
- Conversions(Type casting)
Integers discussion
int8,int16,int32,int64,uint8,uint16,uint32,uint64
Regardless of their size,
int,uint, anduintptrare different types from their explicitly sized siblings. Thusintis not the same type asint32, even if the natural size of integers is 32 bits, and an explicit conversion is required to use anintvalue where anint32is 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
T(x), likeint(x), converts the valuexto typeTif 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
- Architecture-independent
uint8the set of all unsigned 8-bit integers (0 to 255) uint16the set of all unsigned 16-bit integers (0 to 65535) uint32the set of all unsigned 32-bit integers (0 to 4294967295) uint64the set of all unsigned 64-bit integers (0 to 18446744073709551615) int8the set of all signed 8-bit integers (-128 to 127) int16the set of all signed 16-bit integers (-32768 to 32767) int32the set of all signed 32-bit integers (-2147483648 to 2147483647) int64the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) float32the set of all IEEE-754 32-bit floating-point numbers float64the set of all IEEE-754 64-bit floating-point numbers complex64the set of all complex numbers with float32 real and imaginary parts complex128the set of all complex numbers with float64 real and imaginary parts bytealias for uint8runealias for int32- Implementation specific
uinteither 32 or 64 bits intsame size as uintuintptran unsigned integer large enough to store the uninterpreted bits of a pointer value
Covert rune to int and vice versa howto
- As
runeis internally an alias ofint32, we can just convert one into another.
65, 'A'
Assignability discussion
xis assignable to a variable of typeTifx's type is identical toT.x's typeVandThave identical underlying types and at least one ofVorTis not a defined type.Tis an interface type andximplementsT.xis a bidirectional channel value,Tis a channel type,x's typeVandThave identical element types, and at least one ofVorTis not a defined type.xis the predeclared identifiernilandTis a pointer, function, slice, map, channel, or interface type.xis an untyped constant representable by a value of typeT.
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 tomap[string]string, is not toName2. Aliasis assignable to any of those types- As a method receiver should be a defined type, a type alias of primitive types