dongxin2734
2014-08-17 12:01
浏览 208
已采纳

验证struct变量是否为空

First of all, look at the following code snippet:

package main

import (
    "fmt"
)

func main() {

    var para1 struct {
        email, addr string
    }

    para1.email = "test@test.com"

    if para1 != nil {
        fmt.Println(para1)
    }

}

When I compile this code, I've got the compiler error:

./struct_func.go:15: cannot convert nil to type struct { email string; addr string }

How can I validate if my struct variable, if nil or not? Or I have to validate of property like

if para1.email != nil {
    fmt.Println(para1)
}

图片转代码服务由CSDN问答提供 功能建议

首先,请查看以下代码段:

 软件包main 
 
import(
“ fmt” 
)
 
func main(){
 
 var para1 struct {
 email,addr string 
} 
 
 para1.email =“  test@test.com“ 
 
 if para1!= nil {
 fmt.Println(para1)
} 
 
} 
   
 
 

何时 我编译了这段代码,遇到了编译器错误:

  ./ struct_func.go:15:无法将nil转换为struct {email string;  addr字符串} 
   
 
 

如何验证我的struct变量(是否为nil)? 或者我必须验证

 之类的属性,如果para1.email!= nil {
 fmt.Println(para1)
} 
   
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dsmgcse8876 2014-08-17 14:15
    已采纳

    You can compare the struct to its zero value. You can test the string for its zero (empty) value "" or test for string length zero. For example,

    package main
    
    import (
        "fmt"
    )
    
    func main() {
        var para1 struct {
            email, addr string
        }
        para1Zero := para1
        para1.email = "test@test.com"
        if para1 != para1Zero {
            fmt.Println(para1)
        }
        if para1.email != "" {
            fmt.Println(para1.email)
        }
        if len(para1.email) != 0 {
            fmt.Println(para1.email)
        }
    }
    

    Output:

    {test@test.com }
    test@test.com
    test@test.com
    

    The Go Programming Language Specification

    The zero value

    When memory is allocated to store a value, either through a declaration or a call of make or new, and no explicit initialization is provided, the memory is given a default initialization. Each element of such a value is set to the zero value for its type: false for booleans, 0 for integers, 0.0 for floats, "" for strings, and nil for pointers, functions, interfaces, slices, channels, and maps. This initialization is done recursively, so for instance each element of an array of structs will have its fields zeroed if no value is specified.

    点赞 打赏 评论
  • doze79040 2014-08-17 13:58
    var para1 struct {
        email, addr string
    }
    

    After this line, para1 is a struct value containing 2 empty strings as you can see here.

    only slices, maps, channels, pointers and functions can be nil. Therefore comparing para1 to nil does not make sense and the compiler is complaining about that.

    Admittedly the error message could be better.

    If you want to know if para1 is empty you should give its type a name and declare a method on it:

    type Parameter struct {
        email, addr string
    }
    
    func (p Parameter) Empty() bool {
        return (p.email != "" && p.addr != "")
    }
    
    点赞 打赏 评论
  • douyu1990 2015-01-22 22:19

    I know this has been quiet for a while, but a nice reusable way to validate that a struct is empty is to use the "reflect" built-in. It allows you to compare against an empty copy of your struct. Here is a version of your code using an isEmpty function based on reflect.

    package main
    
    import (
        "fmt"
        "reflect"
    )
    
    func isEmpty(object interface{}) bool {
        //First check normal definitions of empty
        if object == nil {
            return true
        } else if object == "" {
            return true
        } else if object == false {
            return true
        }
    
        //Then see if it's a struct
        if reflect.ValueOf(object).Kind() == reflect.Struct {
            // and create an empty copy of the struct object to compare against
            empty := reflect.New(reflect.TypeOf(object)).Elem().Interface()
            if reflect.DeepEqual(object, empty) {
                return true
            }
        }
        return false
    }
    
    func main() {
    
        var para1 struct {
            email, addr string
        }
    
        para1.email = "test@test.com"
    
        if isEmpty(para1) {
            fmt.Println("It's empty")
        } else {
            fmt.Println(para1)
        }
    }
    

    http://play.golang.org/p/geqHKMSWzp

    点赞 打赏 评论

相关推荐 更多相似问题