Okay, I’m new to Go lang, but this doesn’t make sense to me:
package main import ( "fmt" "log" ) var rectLen, rectWidth float64 = 0, 0 func init() { fmt.Println("init is initialized") if rectLen <0 { log.Fatal("rectLen smaller than 0") } if rectWidth <0 { log.Fatal("rectWidht smaller than 0") } } func main() { fmt.Println("Main is initialized") fmt.Println(rectLen, rectWidth) }
This will print:
init is initialized Main is initialized 0 0
Why are 0 and 0 printed when my init function “protects” my rectLen and rectWidth variable should be strictly greater than 0? If I change the value to something less than 0 it works fine and I get:
init is initialized 2009/11/10 23:00:00 rectLen smaller than 0
Thank you!
1> Xiaozhi..:
Because <
is different from “equal to” .Try changing the operator to <=
. This will only be triggered if your value is less than OR equal to 0