I want to create a list of structs that can be returned by a function in a manner as it is in the go-github package.
But what is the correct way to create and populate such a list?
I found two ways, e.g., using the append()
:
...
allowedRepos := strings.Fields("repo1, repo2")
actualRepos := []Repos{}
actualRepos = append(actualRepos, Repos{Name: "repo1", URL: "gth.com/repo1"})
actualRepos = append(actualRepos, Repos{Name: "repo2", URL: "gth.com/repo2"})
...
And by a "direct initialization:
...
actualRepos := []Repos{
Repos{Name: "repo1", URL: "gth.com/repo1"},
Repos{Name: "repo2", URL: "gth.com/repo2"},
}
They works but both look bit awkward and wrong.
So - what is the best way to do it?
It looks like need to create it using pointer but can't make it work.