方法一
/root/code/go/src/contoso.org/book/main.go
package main
import (
"fmt"
"os"
"reflect"
"strings"
)
func main() {
PrintArgs1(os.Args)
}
//变参函数的定义方式
func PrintArgs1(args ...interface{}) {
fmt.Println(args[0].([]string))
fmt.Println(reflect.TypeOf(args[0]))
for k,v := range args[0].([]string) {
fmt.Println(k," =",v,reflect.TypeOf(v))
}
fmt.Println("========================================")
var myStr = (args[0].([]string))[1] //直接点击调试运行,此行代码数组就越界了
fmt.Println("myStr =",myStr,"myStr Type =",reflect.TypeOf(myStr))
ss := strings.Split(myStr,",")
for k,v := range ss {
fmt.Println(k,reflect.TypeOf(v))
}
}
提示,上面的代码写法没有任何错误,但不能直接点击调试运行,因为程序运行时必须传入参数值
[root@contoso ~]# tree $GOPATH/src/contoso.org -L 3 ##查看项目目录结构
/root/code/go/src/contoso.org
├── book
├── deBUG
└── main.go
[root@contoso ~]# cd $GOPATH && go install contoso.org/book && cd ~
[root@contoso ~]# book 141,13,105,17,208
[book 141,208]
[]string
0 = book string
1 = 141,208 string
========================================
myStr = 141,208 myStr Type = string
0 = 141 string
1 = 13 string
2 = 105 string
3 = 17 string
4 = 208 string
[root@contoso ~]#
方法二
单步调试之前,设置好Args的初始化值列表,同样适合上面的范例,直接这样[root@contoso ~]# book 即可运行book
launch.Json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "deBUG",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
@H_419_184@ "args": ["141","13","105","17","208"],
"showLog": true
}
]
}
方法二的简写格式
package main
import (
"fmt"
"os"
"reflect"
)
func main() {
@H_419_184@ for _,arg := range os.Args[1:] {
fmt.Println("arg =",arg," arg Type =",reflect.TypeOf(arg)) // 可以把每个字符串参数转换成我们需要的类型
}
}
调试控制台会输出以下信息:
2017/07/02 14:52:02 server.go:73: Using API v1
2017/07/02 14:52:02 deBUGger.go:97: launching process with args: [/root/code/go/src/contoso.org/book/deBUG 141 13 105 17 208]
API server Listening at: 127.0.0.1:2345
2017/07/02 14:52:03 deBUGger.go:505: continuing
arg = 141 arg Type = string
arg = 13 arg Type = string
arg = 105 arg Type = string
arg = 17 arg Type = string
arg = 208 arg Type = string
方法三
package main
import (
"fmt"
"os"
"reflect"
"strings"
)
func main() {
fmt.Println(strings.Join(os.Args[1:]," ")) @H_419_184@// 传入的数据量很大,一种简单且高效的解决方案是使用strings包的Join函数
args := strings.Split(strings.Join(os.Args[1:]," ")," ")
for _,arg := range args {
fmt.Println("arg =",reflect.TypeOf(arg))
}
}
调试控制台打印输出如下信息:
2017/07/02 11:21:11 server.go:73: Using API v1
2017/07/02 11:21:11 deBUGger.go:97: launching process with args: [/root/code/go/src/contoso.org/book/deBUG 141 13 105 17 208]
API server Listening at: 127.0.0.1:2345
2017/07/02 11:21:12 deBUGger.go:505: continuing
141 13 105 17 208
@H_419_184@arg = 141 arg Type = string arg = 13 arg Type = string arg = 105 arg Type = string arg = 17 arg Type = string arg = 208 arg Type = string
以上是内存溢出为你收集整理的Golang 读取 *** 作系统命令行控制台传入的参数列表os.Args的多种方式全部内容,希望文章能够帮你解决Golang 读取 *** 作系统命令行控制台传入的参数列表os.Args的多种方式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)