Well I am a newby in 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 out:
init is initialized
Main is initialized
0 0
Why is 0 and 0 printed out when my init function is "guarding" that my rectLen, rectWidth variables should be strictly greater than 0? If I change the values to something less than 0, it works fine, I get:
init is initialized
2009/11/10 23:00:00 rectLen smaller than 0
Thanks!
Because <
is not the same as “equal to”. Try changing your operators to <=
. This should fire only if your value is less than OR equal to 0