Tutorial code examples to teach myself Go.
IN PROGRESS!!!
Go concept | Example code |
---|---|
Files | |
code organisation | fibonacci |
get data from stdin | fibonacci |
open file or stdio | wordcount |
bufio scanner | wordcount |
read data from file using fmt.Fscanln | gaussian |
read data from file using bufio scanner | tsp |
io.Writer | trees |
Variables | |
constants (like #define in C) | sieve |
cast int as float64 | pfactor |
runes vs strings | trees |
passing function type as argument to another function | gaussian |
structs and methods | trees |
multiple-valued functions | tsp |
Arrays/slices | |
make a slice with length read from stdin | fibonacci |
initialising a constant-length array | sieve |
static arrays (can't be declared const; use of [...]) | pfactor |
slices and append() | pfactor |
append slices with ... | gaussian |
Loops | |
for loop | fibonacci |
multi-condition for-loop "for a,b := X,Y; ... ; a,b = P,Q { }" | pfactor |
infinite loop for {} | pfactor |
recursion | gcd |
switch | trees |
range | trees |
Command line | |
command line options and flag package | gcd |
command line args | sieve |
cmd line arguments with flag package | coinflip |
--help | gcd |
usage check and os.Exit() | gcd |
Formatting | |
string formatting a la C printf() | fibonacci |
strings package | wordcount |
strconv package | gcd |
convert string to int, float64 etc | sieve |
%v string format for general 'value' | bingcd |
General | |
math package | pfactor |
use of math/rand package | coinflip |
bit ops | bingcd |
timing | bingcd |
parallelisation with goroutines | gaussian |
use of a buffered channel | gaussian |
profiling using pprof | gaussian |