Go多维数组变量的赋值和使用示例(针对os/exec):

huangapple go评论66阅读模式
英文:

Go multidimensonal array variable assignment and usage for os/exec

问题

我正在尝试在golang中创建一个命令数组,并循环遍历它们。我不确定是否应该使用map还是slice,而且我确定我的语法是错误的。这是我的第一个golang应用程序,我在文档中遇到了困难。找不到我的用例。

问题在于变量的赋值。语法不正确,无法编译。

var c2 = [9][3]string{
    [3]string{"/c", "ipconfig", "/all"},
    [3]string{"/c", "ipconfig", "/all"},
    [3]string{"/c", "arp", "-a"},
    [3]string{"/c", "route", "print"},
    [2]string{"/c", "set"},
    [2]string{"/c", "systeminfo"},
    [3]string{"/c", "wmic", "qfe"},
    [3]string{"/c", "tasklist", "/svc"},
    [3]string{"/c", "query", "user"},
}

for i := 0; i < len(c2); i++ {
    x, e := exec.Command("cmd.exe", c2[i][0], c2[i][1], c2[i][2]).Output()
    if e != nil {
        log.Fatal(e)
    }
    fmt.Println(string(x))
}

希望这可以帮助到你!

英文:

I am trying to create an array of commands in golang and loop through them. I am not sure if I should use a map or a slice and I am sure my syntax is wrong. This is my first golang app and I am struggling from the docs. Can't find my use case.

The issue is assigning the variables. The syntax is incorrect and will not compile.

var c2[9][...]string{
            [3]string{&quot;/c&quot;, &quot;ipconfig&quot;, &quot;/all&quot;},
            [3]string(&quot;/c&quot;, &quot;ipconfig&quot;, &quot;/all&quot;),
            [3]string(&quot;/c&quot;, &quot;arp&quot;, &quot;-a&quot;),
            [3]string(&quot;/c&quot;, &quot;route&quot;, &quot;print&quot;),
            [2]string(&quot;/c&quot;, &quot;set&quot;),
            [2]string(&quot;/c&quot;, &quot;systeminfo&quot;),
            [3]string(&quot;/c&quot;, &quot;wmic&quot;, &quot;qfe&quot;),
            [3]string(&quot;/c&quot;, &quot;tasklist&quot;, &quot;/svc&quot;),
            [3]string(&quot;/c&quot;, &quot;query&quot;, &quot;user&quot;),
    }
    for i := range len(c2) { //assign
            x, e := exec.Command(&quot;cmd.exe&quot;, c2[i][0], c2[i][1], c2[i][2]).Output()
            if e != nil {
                    log.Fatal(e)
            }
            fmt.Println(string(x))
    }

答案1

得分: 4

使用切片代替固定长度数组:

var c2 = [][]string{
		{"/c", "ipconfig", "/all"},
		{"/c", "ipconfig", "/all"},
		{"/c", "arp", "-a"},
		{"/c", "route", "print"},
		{"/c", "set"},
		{"/c", "systeminfo"},
		{"/c", "wmic", "qfe"},
		{"/c", "tasklist", "/svc"},
		{"/c", "query", "user"},
	}

然后运行你的命令:

for i := range c2 {
	x, e := exec.Command("cmd.exe", c2[i]...).Output()
	if e != nil {
		log.Fatal(e)
	}
	fmt.Println(string(x))
}
英文:

Instead of using fixed-length arrays, use slices:

var c2 = [][]string{
		{&quot;/c&quot;, &quot;ipconfig&quot;, &quot;/all&quot;},
		{&quot;/c&quot;, &quot;ipconfig&quot;, &quot;/all&quot;},
		{&quot;/c&quot;, &quot;arp&quot;, &quot;-a&quot;},
		{&quot;/c&quot;, &quot;route&quot;, &quot;print&quot;},
		{&quot;/c&quot;, &quot;set&quot;},
		{&quot;/c&quot;, &quot;systeminfo&quot;},
		{&quot;/c&quot;, &quot;wmic&quot;, &quot;qfe&quot;},
		{&quot;/c&quot;, &quot;tasklist&quot;, &quot;/svc&quot;},
		{&quot;/c&quot;, &quot;query&quot;, &quot;user&quot;},
	}

Then, run your commands:

for i := range len(c2) { //assign
      x, e := exec.Command(&quot;cmd.exe&quot;, c2[i]...).Output()
            if e != nil {
                    log.Fatal(e)
            }
            fmt.Println(string(x))
    }

huangapple
  • 本文由 发表于 2022年7月2日 01:42:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/72832929.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定