<Operators in Golang

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

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/operators-in-golang/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: 34331943@QQ.com

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索