mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2024-11-10 00:13:43 +00:00
42 lines
716 B
Go
42 lines
716 B
Go
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
// See page 308.
|
|
//!+
|
|
|
|
// Echo prints its command-line arguments.
|
|
package main
|
|
|
|
import (
|
|
"flag"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
var (
|
|
n = flag.Bool("n", false, "omit trailing newline")
|
|
s = flag.String("s", " ", "separator")
|
|
)
|
|
|
|
var out io.Writer = os.Stdout // modified during testing
|
|
|
|
func main() {
|
|
flag.Parse()
|
|
if err := echo(!*n, *s, flag.Args()); err != nil {
|
|
fmt.Fprintf(os.Stderr, "echo: %v\n", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
func echo(newline bool, sep string, args []string) error {
|
|
fmt.Fprint(out, strings.Join(args, sep))
|
|
if newline {
|
|
fmt.Fprintln(out)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
//!-
|