Types
Table of Contents
- Integers
- Numeric types
- Covert
rune
toint
and vice versa - Assignability
- Type Alias
- Conversions(Type casting)
Integers discussion
int8
,int16
,int32
,int64
,uint8
,uint16
,uint32
,uint64
Regardless of their size,
int
,uint
, anduintptr
are different types from their explicitly sized siblings. Thusint
is not the same type asint32
, even if the natural size of integers is 32 bits, and an explicit conversion is required to use anint
value where anint32
is 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 valuex
to typeT
if 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
uint8
the set of all unsigned 8-bit integers (0 to 255) uint16
the set of all unsigned 16-bit integers (0 to 65535) uint32
the set of all unsigned 32-bit integers (0 to 4294967295) uint64
the set of all unsigned 64-bit integers (0 to 18446744073709551615) int8
the set of all signed 8-bit integers (-128 to 127) int16
the set of all signed 16-bit integers (-32768 to 32767) int32
the set of all signed 32-bit integers (-2147483648 to 2147483647) int64
the set of all signed 64-bit integers (-9223372036854775808 to 9223372036854775807) float32
the set of all IEEE-754 32-bit floating-point numbers float64
the set of all IEEE-754 64-bit floating-point numbers complex64
the set of all complex numbers with float32 real and imaginary parts complex128
the set of all complex numbers with float64 real and imaginary parts byte
alias for uint8
rune
alias for int32
- Implementation specific
uint
either 32 or 64 bits int
same size as uint
uintptr
an unsigned integer large enough to store the uninterpreted bits of a pointer value
Covert rune
to int
and vice versa howto
- As
rune
is internally an alias ofint32
, we can just convert one into another.
65, 'A'
Assignability discussion
x
is assignable to a variable of typeT
ifx
's type is identical toT
.x
's typeV
andT
have identical underlying types and at least one ofV
orT
is not a defined type.T
is an interface type andx
implementsT
.x
is a bidirectional channel value,T
is a channel type,x
's typeV
andT
have identical element types, and at least one ofV
orT
is not a defined type.x
is the predeclared identifiernil
andT
is a pointer, function, slice, map, channel, or interface type.x
is 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
Name1
is assignable tomap[string]string
, is not toName2
. Alias
is assignable to any of those types- As a method receiver should be a defined type, a type alias of primitive types