How to retrieve members of a "complex object" from the reflect.value

I have following sample code:

package main

import (
	"fmt"
	"reflect"
)

type MyResponse struct {
	Code	string
	Status	string
}

type Project struct {
}

func (p *Project) Method1() MyResponse{

	return MyResponse{"LN000001", "Requested by Buyer"}
}

func main() {
	var p Project
	result := reflect.ValueOf(&p).MethodByName("Method1").Call([]reflect.Value{})

	code := <??>
	status := <??>

	fmt.Println("Code: ", code, " Status: ", status)
}

The program should print following output:

Code: LN000001 Status: Requested by Buyer

My query is, what to write in place of “<??>” to achieve this in above sample code ?

Thanks.

response := result[0].Interface().(MyResponse)
code := response.Code
status := response.Status

works for this example.

1 Like

Wonderful !!. It worked brother :bouquet:
Thanks

I found Go’s documentation on how reflection works to be rather skimpy, so I wrote an article about it which may make things clearer.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.