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?
isset
!empty
I looked at other questions and saw the function used len, but this does not work.
len
Go , PHP, , PHP , "" . " " , .
Go/slices , :
if i>=0 && i<len(strings) {...}
, - range(strings) .
range(strings)
, PHP, "isset", :
value, isset := map[index]
index , value , isset ; , value , isset false.
index
value
len() .
len()
, 0. (https://golang.org/ref/spec#Length_and_capacity)