golang[2]-args

go打印命令行参数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
"fmt"
"os"
)

func main() {
var s, sep string
for i := 1; i < len(os.Args); i++ {
s += sep + os.Args[i]
sep = " "
}
fmt.Println(s)
}

测试

1
2
3
$ go build main.go
$ ./main a b c d f
a b c d f

更新2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import (
"fmt"
"os"
)

func main() {
s, sep := "", ""
for _, arg := range os.Args[1:] {
s += sep + arg
sep = " "
}
fmt.Println(s)
}

更新3

1
2
3
4
5
6
7
8
9
10
11
12
package main

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


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