echo.go 495 B

1234567891011121314151617181920212223242526272829
  1. package main
  2. import (
  3. "flag" // command line option parser
  4. "os"
  5. )
  6. var NewLine = flag.Bool("n", false, "print newline") // echo -n flag, of type *bool
  7. const (
  8. Space = " "
  9. Newline = "\n"
  10. )
  11. func main() {
  12. flag.PrintDefaults()
  13. flag.Parse() // Scans the arg list and sets up flags
  14. var s string = ""
  15. for i := 0; i < flag.NArg(); i++ {
  16. if i > 0 {
  17. s += " "
  18. if *NewLine { // -n is parsed, flag becomes true
  19. s += Newline
  20. }
  21. }
  22. s += flag.Arg(i)
  23. }
  24. os.Stdout.WriteString(s)
  25. }