Go语言通常会先定义好结构体,结构体包含了常用的属性,有的时候我们可能需要判定属性是否设置,方法如下:
package main
import "fmt"
type MyStruct struct {
Property string
}
func main() {
s1 := MyStruct{
Property: "hey",
}
s2 := MyStruct{}
if s1.Property != "" {
fmt.Println("s1.Property has been set")
}
if s2.Property == "" {
fmt.Println("s2.Property has not been set")
}
}
注意,如果变量是指针类型,则使用nil判定。
(adsbygoogle = window.adsbygoogle || []).push({});
来源:https://www.02405.com/archives/7908