Go: basic data types
Contents
Basic data types are boolean, numeric, string.
Comparability
- All basic types are comparable using
==and!= - All basic types are ordered by comparison operators
<,>, etc.
Printing
fmt.Printf()uses C-style specifiers- Example:
fmt.Printf("%08b", 1)→ print binary representation of int value 1, padding with zeroes to width of 8 (“00000001”)
Numeric types
- Implicitly signed and unsigned:
int,uint- These are implementation-dependent platform-specific default or natural sizes (either 32 or 64 bits)
intis a different type thanint32, even if they have the same natural size on a given platform. You will still need to convert them.
- Explicitly signed:
int8,int16,int32,int64- Convention is to use these by default; use unsigned for binary operations
- Use two’s complement representation
- Explicitly unsigned:
uint8,uint16,uint32,uint64 float32andfloat64- Convention is to use
float64
- Convention is to use
complex64andcomplex128byteis for raw data, and is equivalent touint8runeis for a Unicode code point and is equivalent toint32- Rune literals are single-quoted Unicode characters:
const x rune = '🤖' - Use
fmt.Printf()with%cto get character representation;fmt.Print()will print the integer code point value
- Rune literals are single-quoted Unicode characters:
uintptris for pointer values (implementation-dependent)math.isNaN()math.NaN()returnsNaN, but don’t compare it: comparisons always yieldfalse
Conversion
- Conversion using e.g.
int():const a int8 = 1; const b int16 = 2; const c int = int(a) + int(b)
Numeric operations
/with two integer operands will truncate- As binary infix operator
^is bitwise XOR - As unary prefix operator,
^is bitwise negation or complement &^is AND NOT (bit clear)- Left-shift zero-pads
- Right-shift of an unsigned value zero-pads
- Right-shift of a signed value pads with the sign bit
Booleans
- Values of
boolaretrueandfalse !is logical negation&&and||have short-circuiting behavior- There is no implicit conversion from boolean values to
0or1or vice versa
Strings
- Immutable sequences of bytes, interpreted as UTF-8 encoded runes
lenreturns byte length, not rune lengthstr[i]returns ith byte where 0 <=i<len(str), not necessary the ith characterstr[i:j]returns bytes from i to jstr[:5]returns from 0 to 5str[5:]returns from 5 to endstr[:]returns a copy ofstr
+is overloaded for string concatenation==,<, etc. compare in lexicographic order, byte by byte- String literals are enclosed in double quotes
- Raw string literals are enclosed in backticks
- They can spread over several lines of source code
- They are useful for writing regexps, HTML templates, etc.
stringsprovides string manipulation functions (e.g.,Contains,Count,Index,HasPrefix,Join)bytesprovides functions for working with byte slices (mutable, but otherwise like strings) — many are counterparts of those instringsstrconvprovides string conversion functions (e.g.,Itoafor int to ASCII.ParseInt,FormatInt)unicodeprovides rune classification functions (e.g.,IsDigit,IsLetter)
Constants
- Use
constkeyword - Convention is to not write constant names in uppercase
Use the iota constant generator to create an enum. Here, a is assigned
the value 0, b the value 1, and c the value 2:
const (
a = iota
b
c
)
Sources
Alan A. A. Donovan and Brian W. Kernighan, The Go Programming Language. Addison-Wesley, 2016, Chapter 3: Basic Data Types