How to use one slice in another slice in golang?

Viewed 55

in this code I want the whole developers slice to be printed in one line. But here is a problem, after using the loop, the slice is divided in multiple times and the {"first project", ...} is repeating multiple times.

Required result

[[first developer second developer third developer] [forth developer fifth developer sixth developer] [first project second project third project forth project]]

Given result

[[first developer second developer third developer] [first project second project third project forth project]]
[[forth developer fifth developer sixth developer] [first project second project third project forth project]]

Code

package main

import "fmt"

func main() {
    developers := [][]string{
        {"first developer", "second developer", "third developer"},
        {"forth developer", "fifth developer", "sixth developer"},
    }

    for i := 0; i < 2; i++ {
        data := [][]string{developers[i], {"first project", "second project", "third project", "forth project"}}
        fmt.Println(data)
    }
}
1 Answers

Please Try the below one

    package main
    
    import "fmt"
    
    func main() {
        developers := [][]string{
            {"first developer", "second developer", "third developer"},
            {"forth developer", "fifth developer", "sixth developer"},
        }
        var data []string
        for i := 0; i < 2; i++ {
            data = append(data, developers[i]...)
        }
        data = append(data, []string{"first project", "second project", "third project", "forth project"}...)
        fmt.Println(data)
    }



Output:
-------
[first developer second developer third developer forth developer fifth developer sixth developer first project second project third project forth project]
Related