This chapter is a tour of the basic components of Go.
- Hello, World
- Command-Line Arguments
- Modify the
echo
program to also printos.Args[0]
, the name of the command that invoked it. - Modify the
echo
program to print the index and value of each of its arguments, on per line. - Experiment to measure the difference in running time between out potentially inefficient versions and the on that uses
strings.Join
- Modify the
Newlines following certain tokens are converted into semicolons, so where newlines are placed matters to proper parsing of Go code. For instance, the poening brace
{
of the function must be on the same line as the end of thefunc
declaration, ont on a line by itself, and in the expressionx+y
, a newline is permitted after but not before the+
operator.
s += sep + os.Args[i]
The statement is an assignment operator; it is equivalent to
s = s + sep + os.Args[i]
The operator +=
is an assignment operator. Each arithmetic and logical operator like +
or *
has a corresponding assignment operator.
The order of map iteration is not specified, but in practice it is random, varying from one run to another. This design is intentional, since it prevents programs from relying on any particular ordering where none is guaranteed.
It helps make input and output efficient and convenient. One of its most useful features is called
Scanner
that reads input and breaks into lines or words; it's often the easiest way to process input that comes naturally in lines.