Next: Finding Duplicate Lines, Previous: The First Echo Command using a for Loop, Up: Tutorial [Index]
Example 1.1: Modify the
echoprogram to also printos.Args[0], the name of the command that invoked it.
// Exercise 1.1 prints the command name and the command-line arguments
package main
import("fmt";"os";"strings")
func main() {
fmt.Println(strings.Join(os.Args[0:], " "))
}
Listing 1.7: exercises/ch1/exercise1.1
Example 1.2: Modify the
echoprogram to print the index and value of each of its arguments, one per line.
// Exercise 1.2 prints each of the command-line arguments with an index, one per line
package main
import("fmt";"os")
func main() {
for index, value := range os.Args[0:] {
fmt.Println(index, value)
}
}
Listing 1.8: exercises/ch1/exercise1.2
Example 1.3: Experiment to measure the difference in running time between our potentially inefficient versions and the one that uses
strings.Join. (Section 1.6 illustrates part of thetimepackage, and Section 11.4 shows how to write benchmark tests for systematic performance evaluation.)