143

I am calling a function that returns an empty array if there are no values.

When I do this it doesn't work:

if r == [] { fmt.Println("No return value") } 

The work around I'm using is:

var a [0]int if r == a { fmt.Println("No return value") } 

But declaring a variable just to check the return value doesn't seem right. What's the better way to do this?

4
  • 6
    Go is not javascript! Read the basics: golang.org/doc/effective_go.html Commented Jul 1, 2016 at 11:19
  • 6
    More things - first - array is not a slice. Array is type, that has fixed number of elements, therefore question makes sense only for slice. Second - it won't work, if you initialize those slices. Third - and what about if len(a) == 0? Commented Jul 1, 2016 at 11:24
  • Does your workaround compile? I can't see how it would unless r is also of type [0]int but in that case the two (empty) arrays would always compare equal. Commented Jul 1, 2016 at 13:16
  • @PaulHankin Yes it does work. The return type of the function is int[] Commented Jul 3, 2016 at 15:03

3 Answers 3

180

len() returns the number of elements in a slice or array.

Assuming whatever() is the function you invoke, you can do something like:

r := whatever() if len(r) > 0 { // do what you want } 

or if you don't need the items

if len(whatever()) > 0 { // do what you want } 
Sign up to request clarification or add additional context in comments.

5 Comments

The problem with this approach is that a slice which is just declared, like, var num []int, also returns the len to be zero. i.e, len(num) == 0
@nawaz yes but that’s expected is it not? Even in case var num []int = nil then len(num) returns 0. This is just perfect imo. This way I don’t need to do a nil check beforehand. If I do need to check for nil I can just add that explicitly. But again to the point of uninitaliazed var, agree it should be 0. since len can only return an int then this makes perfect sense. A len return val of -1 would be very confusing to see imo.
Do you know whether len traverses the whole slice in order to compute the length (and therefore its time complexity would be equal to the length of the slice)? Or is it constant time?
@AlexM. len operates in constant time. The length of the slice is stored as part of the slice, next to capacity and the pointer to the data.
@rv.kvetch how is nil having a valid length perfect? Anything other than a nil pointer exception in this case seems needlessly obscure.
27

You can just use the len function.

if len(r) == 0 { fmt.Println("No return value") } 

Although since you are using arrays, an array of type [0]int (an array of int with size 0) is different than [n]int (n array of int with size n) and are not compatible with each other.

If you have a function that returns arrays with different lengths, consider using slices, because function can only be declared with an array return type having a specific length (e.g. func f() [n]int, n is a constant) and that array will have n values in it (they'll be zeroed) even if the function never writes anything to that array.

Comments

8

You can use the inbuilt function provided by Golang

len()

It will helps you to easily find a slice is empty or not.

if len( yourFunction() ) == 0 { // It implies that your array is empty. } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.