Test index out of golang range

Sorry for this noob question, but I'm not sure how I check if the element I'm viewing is available for an array, consider the following contrived code:

func main() {
    strings := []string{"abc", "def", "ghi", "jkl"}
    for i := 0; i<5; i++ {
        if strings[i] {
            fmt.Println(strings[i])
        }
    }
}

https://play.golang.org/p/8QjGadu6Fu

I obviously go beyond, but I'm not sure how I am testing to prevent a bug. I'm used to PHP, where would I use a test issetor !emptydoes it have such a thing?

I looked at other questions and saw the function used len, but this does not work.

+4
source share
2 answers

Go , PHP, , PHP , "" . " " , .

Go/slices , :

if i>=0 && i<len(strings) {...}

, - range(strings) .

, PHP, "isset", :

value, isset := map[index]

index , value , isset ; , value , isset false.

+15

All Articles