This question already has an answer here:
Is there a generic way to test a variable for initial in Go?
Given these test:
package main
import "fmt"
type FooStruct struct {
A string
B int
}
type BarStruct struct {
A string
B int
C map[int]string
}
func main() {
// string isinital test
var s string
fmt.Println(s == "")
// int isinital test
var i int
fmt.Println(i == 0)
// primitive struct isinital test
var fp FooStruct
fmt.Println(fp == FooStruct{})
// complex struct isinital test
// fail -> invalid operation: fc == BarStruct literal (struct containing map[int]string cannot be compared)
var fc BarStruct
fmt.Println(fc == BarStruct{})
// map isinital test
var m map[string]int
fmt.Println(len(m) == 0)
// map isinital test
// fail -> invalid operation: m == map[string]int literal (map can only be compared to nil)
fmt.Println(m == map[string]int{})
}
A: What is the proper way to test BarStruct
for initial?
B: Is there a generic way to test any var for is initial?
</div>