我具有以下结构,该结构是我手动创建的app service runner etc
func Cmr(mPath string) [][]string {
cav := [][]string{
{mPath, "app", "app2"},
{mPath, "service"},
{mPath, "runner1", "runner2", "runner3"},
}
return cav
}
现在我需要从此输入中创建此结构,我的意思是返回相同的结构'cav`
现在,我还有另一个函数可以返回array of string
名称cmdList
每行之间在app app2 appN
0 = app app2
1 = service
2 = runner1 runner2 runner3
如何获取上面的字符串数组并将其作为函数Cmr
的参数
然后删除硬编码值,然后从cmdList
中获取它们,而不是硬编码...
像
func Cmr(mPath string,cmdList []string) [][]string {
cav := [][]string{
{mPath, cmdList[0], "app2"},
{mPath, "service"},
{mPath, "runner1", "runner2", "runner3"},
}
return cav
}
更新 最后应该是这样的 except ,我不知道如何用空格定界符
分隔cmdList
的条目
func Cmr(mPath string, cmdList []string) [][]string {
cav := [][]string{}
cav = append(cav, append([]string{mPath}, cmdList[0]))
cav = append(cav, append([]string{mPath}, cmdList[1]))
cav = append(cav, append([]string{mPath}, cmdList[2]))
return cav
}
这将创建类似(因为我不处理空格分隔符)
cav := [][]string{
{mPath, "app app2"},
{mPath, "service"},
{mPath, "runner1 runner2 runner3"},
}
但我需要
cav := [][]string{
{mPath, "app", "app2"},
{mPath, "service"},
{mPath, "runner1", "runner2", "runner3"},
}
答案 0 :(得分:0)
我想您正在寻找类似的东西:
cav = append(cav, strArray)
其中strArray
是您从cmdList()
收到的字符串数组
编辑:这最终就是他们想要的内容:https://play.golang.org/p/ZFFhRRu43Em
答案 1 :(得分:0)
基本上,从我看来,您应该按参数获取这些字符串切片,并将它们附加到您的[][]string
上。
这是一个完整的示例,其中将它们传递给函数:
package main
import "fmt"
func Cmr(mPath string, appList, serviceList, runnerList []string) [][]string {
cav := [][]string{}
cav = append(cav, append([]string{mPath}, appList...))
cav = append(cav, append([]string{mPath}, serviceList...))
cav = append(cav, append([]string{mPath}, runnerList...))
return cav
}
func main() {
cmr := Cmr(
"mpathTestValue",
[]string{"app1", "app2"},
[]string{"service1", "service2"},
[]string{"runner1", "runner2", "runner3"},
)
for _, values := range cmr {
fmt.Println(values)
}
}
提供输出
[mpathTestValue app1 app2]
[mpathTestValue service1 service2]
[mpathTestValue runner1 runner2 runner3]
您可以尝试自己玩here