General notes about learning Go, especially while reading through Learning Go - 2nd edition. I’m trying to keep the notes here minimal.
Commands
- specify the name of the binary that is created -
go build - o hello
- run a go file without creating a binary -
go run hello.go
Predeclared Types
- it’s possible to put underscores in the middle of an integer or floating point literal to improve readablilty, for example to group thousands
10_2500
- a
rune
literal represents a character and is surrounded by single quotes. Special ones:'\t'
for tab'\n'
for newline'\\'
for backslash
string
literal -"text in double quotes"
or araw string literal
in backquotes`text in a raw string literal`
. In a raw string literal we can use backslashes, double quotes, and newlines without escaping them.byte
is an alias foruint8
,int
isint32
orint64
depending on the CPU architecture,uint
is always 0 or positive. Unless we have a good reason to use some other type, just useint
.- Integers have the normal operators
+ - * /
and<< >>
for bit-manipulation, also for examplex += 5
to immediately assign the variable - Floating point types
float32
&float64
, generally - usefloat64
. Floating point numbers cannot represent a decimal value exactly, don’t use them to represent things like money for example. string
- can be compared using==
, checking for difference using!=
or ordering with> >= < <=
. They are concatenated using+
. Strings are immutable - the value of a string variable can be changed but not the value of the string that is assigned to itrune
- anint32
in the background, used to refer to a single character.- Go doesn’t have automatic type conversion, this must be done manually. Example:
var x int = 10 var y float64 = 30.2 var sum1 float64 = float64(x) + y var sum2 int = x + int(y) fmt.Println(sum1, sum2)
- No type (other than
bool
) can be converted tobool
. If we want to use another data type as a boolean, we have to use one of the comparison operators, for examplex == 0
ors == ""
- Literals are untyped. So if the type or a varaible is compatible with the literal, we can just assign it.
var x float64 = 10
orvar y float64 = 200.3 * 5
. However, we can’t for example assign a literalstring
to afloat
orint
and vice versa. Also we still need to consider size limitations.
Variables
-
Declaring variables:
var x int = 10
var x = 10
var x int
var x, y int = 10, 20
var x, y int
var x, y = 10, "hello"
-
Or, using a declaration list:
var ( x int y = 20 z int = 30 d, e = 40, "hello" f, g string )
-
Within a function - we can use short declaration:
x := 10
x,y := 10, "hello"
- When initializing a variable to its zero value, we should use
var x int
to make it clear that this is intended - Also, when we want to use a type that isn’t the default type for the constant, use the long form
var x byte = 20
- Generally, only declare multiple variables on one line when assigning multiple values returned from a function
- Overall, try not to use variables outside of functions
Stopped at “Using const” chapter
Constants
- Set as
const x int64 = 10
, can be declared at the package level or within a function and it is not possible to change the value after it has been assigned - Can hold only values that the compiler can figure out at compile time
- Mostly used as a way to give names to literals
- Can be
untyped
, the type is inferred -const x = 10
(so this could be used as int, float64 or byte for example)
Stopped at Unused variables chapter.