I want return string or slice at one function, I can use in php. but, get some error in Go
package main
import "fmt"
func main() {
// arr := []int {34, 23, 45, 56, 62, 45, 12, 96, 22}
arr := []int{}
fmt.Println(test(arr))
}
func test(dataList []int ) ( string , []int ) {
if dataList == nil{
return "this is string"
}
return []int{}
}
error: not enough arguments to return
If you define two return types, you always need to return the two types. You can not return only one.
Have a look at this:
func test(dataList []int) (string, []int) {
if dataList == nil {
return "this is string", []int{}
}
return "", []int{}
}
This would work, because I am returning both a string
and an []int
in every case (and in exact the defined order).
Maybe if you can explain what your function tries to accomplish, we can give better advice on how to design the function.