Here is a piece of my code in Go
ips := strings.Split(IP, ",")
if len(ips) < 1 {
return fmt.Errorf("'%v' ip is wrong", ips)
}
I tested this and it seems that ips
variable can never be nil. For example, an empty string produces a slice of empty string (length 1).
Do you think I can remove the if
block?
You are right: strings.Spit()
will never return a nil value. The result will be of type []string
with at least one element containing the given string.
Whether you can remove the if block depends: Does your code have a problem if len(ips) < 2
? If it does not you can safely remove the if block.
If however e.g. you are only interested in ips[1]
then you definitely need to check first.