Go in 30 lessons
Go compiles a program into one static file. You build it in Bucket & Code with the Go runtime and run it on any QNAP NAS with the same type of processor.
TurboShell does not contain Go. These programs are made in Bucket & Code with its Go runtime; the finished file runs on the NAS without Go.
The 30 lessons
Chapter 1
First steps
Print text, keep values in variables, calculate and read what the user types.
- 1Build a programA program has
package mainandfunc main. - 2Declare variables
varcan name a type,:=lets Go choose it. - 3Print in columns
Printffills a pattern:%stext,%dwhole numbers,%.1fone digit after the point,%6.1fthe same in six columns,%vany value. - 4Count with numbersDivision of whole numbers drops the fraction;
float64(x)keeps it. - 5Work with textThe package
stringscuts and searches. - 6Ask the userA
bufio.Scannerreads lines,strconv.Atoiturns text into a number and says when it cannot.
Chapter 2
Decisions and loops
Use conditions to choose what to do and loops to repeat it.
- 7Decide with if
if,else ifandelsechoose a block. - 8Choose with switch
switchcompares a value, or takes the first true case. - 9Repeat with forGo has one loop word.
- 10Write functionsA function names the types of its parameters and results and may return several values.
- 11Handle errorsAn error is a value you check at once.
- 12Keep values in a slice
appendadds,[:2]takes a part, the packageslicessorts and searches.
Chapter 3
Data, functions and the command line
Keep many values together, write functions and read the command line.
- 13Count with a mapA map finds a value by its name.
- 14Join fields into a structA struct keeps the slot, model and temperature of one disk together.
- 15Give a struct methodsA method belongs to a type.
- 16Use an interfaceA type with the method
Check() (bool, string)fits the interface, so one loop handles them all. - 17Pass an addressA function gets a copy.
- 18Read optionsThe package
flagreads-limit 95and-quiet;flag.Arggives one of the other arguments.
Chapter 4
Files and errors
Read and write files and handle errors such as a missing file.
- 19Write and read a file
os.WriteFilewrites the whole file,O_APPENDadds to its end, aScannerreads line by line. - 20Read and write JSON
json.MarshalIndentturns a struct into JSON andjson.Unmarshalturns JSON into a struct. - 21Build a calculatorA function returns a result or an error;
mainreads the lines and prints what came back. - 22Count words from standard inputA
Scannercan split by words. - 23Copy a file with a date
time.Now().Formatmakes the name,io.Copycopies andO_EXCLnever overwrites a file. - 24Walk through folders
filepath.WalkDirvisits every file,slices.SortFuncputs the largest first.
Chapter 5
Tools for the NAS
Programs that read the state of the NAS and report it.
- 25Ask the system for free space
syscall.Statfsasks the system about the volume of a folder: how many blocks it has and how many are free. - 26Run a command
exec.Commandnames a program and its arguments,Outputruns it and gives what it printed, and an error tells the exit code. - 27Do work side by side
WaitGroup.Gostarts each goroutine,Waitwaits for all of them and a channel limits how many work at once. - 28Summarise a log
SplitNcuts a line into at most four parts, so the last one is the whole message. - 29Build a notepad
load,saveandrundivide the work; each returns an error to its caller. - 30Finish a toolOne file reads volumes, disks and a log and prints text or a web page made from a template.
Projects
Longer programs that draw a menu, play a game, greet you at sign-in or show the state of the NAS. Each page shows the screen as the program drew it on a test NAS.
- A framed menu in Go with raw keysA Go program puts the terminal into raw mode, draws a framed menu with ANSI escapes and opens boxes with the NAS's memory, volumes, uptime and load. You learn raw keys without extra packages and how to give the terminal back its settings.
- A login greeting with text/templateA Go program fills a text/template with the facts of the NAS (greeting, model, uptime, CPU, load, free space, tip of the day) and frames the result. You learn templates with your own functions and a struct as their data.
- A NAS summary on screen and as JSONA Go program collects the CPU, temperature, memory, data volumes and network ports of the NAS into structs and prints them as a screen with bars, or with -json as JSON. You learn structs with json tags, the flag package and one source of facts for two outputs.
- Packet catcher: a game with goroutinesIn this Go game packets fall down the screen and you catch them with a switch moved by the arrow keys; three missed packets end the game. You learn goroutines, channels and select, with one goroutine for the clock and one for the keys.
- A status page of the NAS with net/httpA Go program serves the model, uptime, CPU, load, memory and data volumes of the NAS as a web page that reloads every 5 seconds, so you can watch it from any browser on your network. You learn net/http, html/template and a clean stop on Ctrl+C.
- Find duplicate files with a pool of workersA Go program finds duplicate files in a folder of the NAS. It compares sizes first, hashes only the files that share a size with SHA-256 in a pool of goroutines, and shows each group with the space its extra copies take. You learn worker pools, channels and sync.WaitGroup.
Write it
Open the terminal of Bucket & Code's editor and type these commands. Then make the file 29-notepad.go in this folder in the editor.
mkdir -p lessons
cd lessonsRun it
The first line puts Go on the path of this terminal. go build makes the program 29-notepad.
eval "$(bvs use go-1.27-x86_64)"
go build 29-notepad.go
./29-notepad add "Buy two disks"
./29-notepad list