Confirm if an empty variable is struct or not.

First of all, see 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 have a compiler error:

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

How can I check if my variable is struct if nil or not? Or I need to check a property like

 if para1.email != nil { fmt.Println(para1) } 
+5
source share
3 answers

You can compare a struct with its null value. You can check string for zero ("empty) "" value or check for string length zero. For instance,

 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) } } 

Conclusion:

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

Go programming language specification

Zero value

When memory is allocated to store a value, either through a declaration or a call to make or new, and explicit initialization, the memory receives initialization by default. each element of this value is set to zero for its type: false for booleans, 0 for integers, 0.0 for float, "" for strings and nil for pointers, functions, interfaces, fragments, channels and maps. This initialization is performed recursively, so that, for example, each element in the array of structures will have its fields zeroed if no value is specified.

+5
source

I know that for a while it was quiet, but a good reusable way to verify that the structure is empty is to use the built-in β€œreflect”. It allows you to compare with an empty copy of your structure. Below is the version of your code using the reflection-based isEmpty function.

 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 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 empty") } else { fmt.Println(para1) } } 

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

+3
source
 var para1 struct { email, addr string } 

After this line, para1 is a structure value containing two empty lines as you can see here .

only fragments, maps, channels, pointers and functions can be nil . Therefore, comparing para1 with nil does not make sense, and the compiler complains about it.

Admittedly, the error message might be better.

If you want to know if para1 empty, you must specify its name and declare a method on it:

 type Parameter struct { email, addr string } func (p Parameter) Empty() bool { return (p.email != "" && p.addr != "") } 
+1
source

Source: https://habr.com/ru/post/1200527/


All Articles