Preface
Using any programming language will encounter the problem of nullification, so how does Golang determine the nullability of a custom structure type?
In fact, the empty structure is not a simple comparison with nil. Please look at the following two methods:
package main
import (
"fmt"
"reflect"
)
type A struct {
name string
age int
}
func (a A) IsEmpty() bool {
return reflect.DeepEqual(a, A{})
}
func main() {
var a A
if a == (A{}) { // Brackets cannot be removed
fmt.Println("a == A{} empty")
}
if a.IsEmpty() {
fmt.Println("reflect deep is empty")
}
}
Read More:
- [Go] Can structure/structure pointer be compared with operator == is not defined error
- How to Test whether the property value of each property in the object is not empty
- Golang: How to Read File All Content in one time
- How to Set Time Zone in Golang
- How to Solve Converting circular structure to JSON‘ Error
- JS to determine whether the string contains a character
- [Solved] it only responds to error and does not enter success after AJAX is successfully processed
- [Solved] TypeError: Converting circular structure to JSON – JSON.stringify
- ‘webpack dev server’ is not an internal or external command, nor is it a runnable program or batch file. Solution: error in cnpm run dev:
- [Go]Understand the golang project performance analysis tool PProf
- [Solved] Angular basic create component error: Is it missing an @NgModule annotation
- [Go]Understand the golang project performance analysis tool trace
- How to Solve “Vue is not defined” Error
- JS to find the last character of the string and remove it
- How to Solve OffsetParent is not set error
- ERROR in [eslint] ESLint is not a constructor (How to Solve)
- Error: Computed property “menuList” was assigned to but it has no setter.
- [Solved] Error: #error More than 1 blank line not allowed no-multiple-empty-lines
- After Vite starts, it will prompt “network: use ` — host ` to expose”, and the service cannot be accessed through network IP
- [Solved] To install it, you can run: npm install –save core-js/modules/es.error.cause.js