Previous: , Up: The First Echo Command using a for Loop   [Index]


1.5.4 The function Join in the strings Package

If the amount of data involved is large, this could be costly. A simpler and more efficient solution would be to use the Join function from the strings package:

// Echo3 uses Join function to create a string
package main

import("fmt";"os";"strings")

func main() {
        fmt.Println(strings.Join(os.Args[1:], " "))
        fmt.Println(os.Args[1:])
}

Listing 1.6: gopl.io/ch1/echo3

Finally, if we don’t care about format but just want to see the values, perhaps for debugging, we can let Println format the results for us:

fmt.Println(os.Args[1:])

The output of this statement is like what we would get from strings.Join, but with surrounding brackets. Any slice may be printed this way.