NASutils Knowledge BaseUnofficialPolski

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.

Start: Build a program

Programming on the NAS

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.

  1. 1Build a programA program has package main and func main.
  2. 2Declare variablesvar can name a type, := lets Go choose it.
  3. 3Print in columnsPrintf fills a pattern: %s text, %d whole numbers, %.1f one digit after the point, %6.1f the same in six columns, %v any value.
  4. 4Count with numbersDivision of whole numbers drops the fraction; float64(x) keeps it.
  5. 5Work with textThe package strings cuts and searches.
  6. 6Ask the userA bufio.Scanner reads lines, strconv.Atoi turns 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.

  1. 7Decide with ifif, else if and else choose a block.
  2. 8Choose with switchswitch compares a value, or takes the first true case.
  3. 9Repeat with forGo has one loop word.
  4. 10Write functionsA function names the types of its parameters and results and may return several values.
  5. 11Handle errorsAn error is a value you check at once.
  6. 12Keep values in a sliceappend adds, [:2] takes a part, the package slices sorts and searches.

Chapter 3

Data, functions and the command line

Keep many values together, write functions and read the command line.

  1. 13Count with a mapA map finds a value by its name.
  2. 14Join fields into a structA struct keeps the slot, model and temperature of one disk together.
  3. 15Give a struct methodsA method belongs to a type.
  4. 16Use an interfaceA type with the method Check() (bool, string) fits the interface, so one loop handles them all.
  5. 17Pass an addressA function gets a copy.
  6. 18Read optionsThe package flag reads -limit 95 and -quiet; flag.Arg gives one of the other arguments.

Chapter 4

Files and errors

Read and write files and handle errors such as a missing file.

  1. 19Write and read a fileos.WriteFile writes the whole file, O_APPEND adds to its end, a Scanner reads line by line.
  2. 20Read and write JSONjson.MarshalIndent turns a struct into JSON and json.Unmarshal turns JSON into a struct.
  3. 21Build a calculatorA function returns a result or an error; main reads the lines and prints what came back.
  4. 22Count words from standard inputA Scanner can split by words.
  5. 23Copy a file with a datetime.Now().Format makes the name, io.Copy copies and O_EXCL never overwrites a file.
  6. 24Walk through foldersfilepath.WalkDir visits every file, slices.SortFunc puts the largest first.

Chapter 5

Tools for the NAS

Programs that read the state of the NAS and report it.

  1. 25Ask the system for free spacesyscall.Statfs asks the system about the volume of a folder: how many blocks it has and how many are free.
  2. 26Run a commandexec.Command names a program and its arguments, Output runs it and gives what it printed, and an error tells the exit code.
  3. 27Do work side by sideWaitGroup.Go starts each goroutine, Wait waits for all of them and a channel limits how many work at once.
  4. 28Summarise a logSplitN cuts a line into at most four parts, so the last one is the whole message.
  5. 29Build a notepadload, save and run divide the work; each returns an error to its caller.
  6. 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.

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 lessons

Run 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

The programs to download