doukuang6795 2018-09-20 19:24
浏览 41
已采纳

将结构从字符串数组更改为深度数组

I’ve the following structure which I created manually the values like app service runner etc

func Cmr(mPath string) [][]string {
    cav := [][]string{
        {mPath, "app", "app2"},
        {mPath, "service"},
        {mPath, "runner1", "runner2", "runner3"},
    }
    return cav
}

Now I need from this input to create this struct, I mean to return the same structure ‘cav`

Now I’ve other function which returns array of string name cmdList each line have a space delimiter between values like app app2 appN

0 = app app2
1 = service
2 = runner1 runner2 runner3 

How can I take the above array of string and put it as parameter to the function Cmr And remove the hard-coded value and get them from the cmdList instead of hard-coded them...

Like

func Cmr(mPath string,cmdList []string) [][]string {
    cav := [][]string{
        {mPath, cmdList[0], "app2"},
        {mPath, "service"},
        {mPath, "runner1", "runner2", "runner3"},
    }
    return cav
}

update at the end it should be something like this except the I dont know how to split the entry of the cmdList with the space delimiter

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
}

This will create something like (since Im not handling the space delimiter)

   cav := [][]string{
        {mPath, "app app2"},
        {mPath, "service"},
        {mPath, "runner1 runner2 runner3"},
    }

But I need

  cav := [][]string{
        {mPath, "app", "app2"},
        {mPath, "service"},
        {mPath, "runner1", "runner2", "runner3"},
    }
  • 写回答

2条回答 默认 最新

  • dousiyou1058 2018-09-20 19:36
    关注

    I imagine you're looking for something like:

    cav = append(cav, strArray)
    

    where strArray is the array of strings you've received from cmdList()

    EDIT: this ended up being what they were looking for: https://play.golang.org/p/ZFFhRRu43Em

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?