Go – unexported field or method for go interface

go

I am quite new to go, and I am trying to loop through each field of an interface (can be different struct) but I am not sure what is going wrong with the following code? it reports runtime error: panic: reflect.Value.Interface: cannot return value obtained from unexported field or method

what are those unexported field/method? I have the SomeField field capitalized thanks

type SomeStruct struct {
SomeField   uint32
}

func test(obj interface{}){
  typ := reflect.TypeOf(obj)
  val := reflect.ValueOf(obj)
  for i := 0; i < typ.NumField(); i++ {
    fieldValue := val.Field(i).Interface()
    fmt.Println(fieldValue)
  }
}

func test1(obj interface{}){
  val := reflect.ValueOf(obj)
  test(val)
}

func main() {
  var ss SomeStruct
  test1(ss)
}

Best Answer

in Go struct,field started in lowercase means its scope is private,while uppercase means public. So,you should keep the field started with UpperCase alpha.