I have a struct that will get its value from user input.
Now I want to extract only field names that have associated values. Fields with a nil
value should not be returned. How can I do that?
Here’s my code:
package main
import "fmt"
import "reflect"
type Users struct {
Name string
Password string
}
func main(){
u := Users{"Robert", ""}
val := reflect.ValueOf(u)
for i := 0; i < val.NumField(); i++ {
fmt.Println(val.Type().Field(i).Name)
}
}
Current Result:
Name
Password
Expected result:
Name