Next: , Previous: , Up: Tutorial   [Index]


1.5 The First Echo Command using a for Loop

List of Imports

Here’s an implementation of the Unix echo command, which prints its command-line arguments on a single line. It imports two packages, which are given as a parenthesized list rather than as individual import declarations. Either form is legal, but conventionally the list form is used. The order of imports doesn’t matter; the gofmt tool sorts the package names into alphabetical order.

// Echo1 prints its command-line arguments.
package main

import("fmt";"os")

func main() {
        var s, sep string
        for i := 1; i < len(os.Args); i++ {
                s += sep + os.Args[i]
                sep = " "
        }
        fmt.Println(s)
}

Listing 1.2: gopl.io/ch1/echo1

Comments

Comments begin with ‘//’. All text from a ‘//’ to the end of the line is commentary for programmers and is ignored by the compiler. By convention, we describe each package in a comment immediately preceding its package declaration; for a main package, this comment is one or more complete sentences that describe the program as a whole.

var Declaration

The var declaration declares two variables s and sep, of type ‘string’. A variable can be initialized as part of its declaration. If it is not explicitly initialized, it is implicitly initialized to the zero value for its type, which is 0 for numeric types and the empty string ‘""’ for strings. Thus in this example, the declaration implicitly initializes s and sep to empty strings. We’ll have more to say about variables and declarations in Chapter 2.

Operators

For numbers, Go provides the usual arithmetic and logical operators. When applied to strings, however, the ‘+’ operator concatenates the values, so the expression

sep + os.Args[i]

represents the concatenation of the strings sep and os.Args[i]. The statement we used in the program,

s += sep + os.Args[i]

is an assignment statement that concatenates the old value of s with sep and os.Args[i] and assigns it back to s; 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 echo program could have printed its output in a loop one piece at a time, but this version instead builds up a string by repeatedly appending new text to the end. The string s starts life empty, that is, with value ‘""’, and each trip through the loop adds some text to it; after the first iteration, a space is also inserted so that when the loop is finished, there is one space between each argument. This is a quadratic process that could be costly if the number of arguments is large, but for echo, that’s unlikely. We’ll show a number of improved versions of echo in this chapter and the next that will deal with any real inefficiency.

Short Variable Declaration

The loop index variable i is declared in the first part of the for loop. The ‘:=’ symbol is part of a short variable declaration, a statement that declares one or more variables and gives them appropriate types based on the initializer values; there’s more about this in the next chapter.

Increment and Decrement Statements

The increment statement ~i++~ adds 1 to ~i~; it’s equivalent to ~i += 1~ which is in turn equivalent to ~i = i + 1~. There’s a corresponding texinfo:@dfn{decrement}@@ statement i-- that subtracts 1. These are statements, not expressions as they are in most languages in the C family, so j = i++ is illegal, and they are postfix only, so --i is not legal either.


Next: Exercises, Previous: Working with Command-Line Arguments, Up: Tutorial   [Index]