我试图判断我的整数值或浮点值是否为空.但是类型错误被抛出.
已try :
if foo == nil
//Error: cannot convert nil to type float32
//all other methods I Tried also throw type errors too
我试图判断我的整数值或浮点值是否为空.但是类型错误被抛出.
已try :
if foo == nil
//Error: cannot convert nil to type float32
//all other methods I Tried also throw type errors too
整数和浮点数的zero values是0.nil
不是有效的整数值或浮点值.
指向整数或浮点数的指针可以是nil
,但不能是它的值.
这意味着您可以判断零值:
if foo == 0 {
// it's a zero value
}
或者你要处理指针:
package main
import (
"fmt"
)
func main() {
var intPointer *int
// To set the value use:
// intValue := 3
// intPointer = &intValue
if intPointer == nil {
fmt.Println("The variable is nil")
} else {
fmt.Printf("The variable is set to %v\n", *intPointer)
}
}