TRY AND ERROR

気になったこと、勉強したこと、その他雑記など色々メモしていきます。。Sometimes these posts will be written in English.,

How to check if index is exist in Array or Slice in Golang?

I guess in Golang, there isn't any method to check that index is exist in Slice, for PHP like "isset()".
So I take a method I made like below.

func isset(arr []string, index int) bool {
    return (len(arr) > index)
}

By the way, in Golang when you check that index is exist in array and then also check that value of index of array is not blank"", it works like below.

// Assume "len(arr) == 2"  in this time
if (isset(arr, 3) && arr[3] != "") {
    // Any code.
}

Though the right side divided with "&&" looks likely to throw the error "out of range", the left side is false that's why the right side is ignored in Golang.


You have any ideas about "isset()"??