I am coming from javascript and know how to check if a variable exists. We can use !!var
I have come across an array in Go where I want to know if an index exists:
myArr := []int{1, 2, 3}
if myArr[3] {
fmt.Println("YES")
}
When I run this it gives me an error: Index Out Of Range: 3
Since Go is a compiled language the concept of a variable not existing does not make sense. The closest thing is that some types can take a nil value.
As far as arrays go they just have a length (without gaps). So if the length is N then only indices 0 to N-1 are valid. The built-in len() function works with any array or slice.