If you have to return an empty slice (of any type) there are three ways to do it.
- Return a direct slice:
func getNames() []string {
return []string{}
}
2. Return a variable:
func getNames() []string {
var names []string
return names
}
3. Return the variable initialized in the function declaration and you don’t have to say what to return:
func getNames() (names []string) {
return
}
Note: The second and the third solution are the same.