命令行参数

    1. package main
    2. import (
    3. "flag"
    4. "os"
    5. )
    6. var NewLine = flag.Bool("n", false, "print newline")
    7. const (
    8. Space = " "
    9. Newline = "\n"
    10. )
    11. func main() {
    12. flag.PrintDefaults()
    13. flag.Parse()
    14. var s string = ""
    15. for i := 0; i < flag.NArg(); i++ {
    16. if i > 0 {
    17. s += " "
    18. if *NewLine {
    19. s += Newline
    20. }
    21. }
    22. s += flag.Arg(i)
    23. }
    24. os.Stdout.WriteString(s)
    25. }