Reputation: 59
Example:
I have a struct,
type Person struct {
Name string
DOB string
age int
}
func detectUnexportedInStruct(i interface{}) bool {
// if `i` contains unexported fields return true
// else return false
}
func main() {
fmt.Println(detectUnexportedInStruct(Person{Name: "", DOB: ""}))
}
I would want true
to be printed because Person contains an unexported field age
.
How can I build my detectUnexportedInStruct()
?
Upvotes: 1
Views: 289
Reputation: 38303
Use the reflect
package:
func detectUnexportedInStruct(i interface{}) bool {
rt := reflect.TypeOf(i)
for i := 0; i < rt.NumField(); i++ {
if !rt.Field(i).IsExported() {
return true
}
}
return false
}
https://play.golang.org/p/f_JVLWYavjm
Upvotes: 4