Go Types
Basic types
bool
,
string
,
int
, int8
, int16
, int32
, int64
,
uint
, uint8
, uint16
, uint32
, uint64
, uintptr
,
byte
, alias for uint8
,
rune
, alias for int32
, represents a Unicode code point
float32
, float64
,
complex64
, complex128
Default values
Type | Default Value |
---|---|
int |
0 |
float |
0.0 |
string |
"" |
bool |
false |
Define and assign variables
Syntax: var isTeslaGreat bool = false
We use the var
keyword, followed by the variable name, then the type. In this case it's a Boolean (bool
) keyword. Optionally we can assign a value, which in this case we assign the value false
to the variable isTeslaGreat
.
Constants
Syntax: const Pi = 3.142
Constants are declared like variables, but with the const keyword.
Constants can be character, string, boolean, or numeric values.
Constants cannot be declared using the := syntax.
Inferred types
Syntax: isTeslaGreat := false
Alternative Syntax: var isTeslaGreat = false
Go can infer types based on the assigned values.
floats
created this way are float32
, int
s are either int32
or int64
.