lundi 2 mars 2020

How to use reflection set a customer field value in go?

Is there have any way use reflection to set a customer filed value. My code below.

type Credential{
Client *credentialClient
}

// Reflection
s := &Credential{}
a := reflect.ValueOf(s)
a = a.Elem()

// get the client filed I want to set
client := a.FieldByName("Client")

// c is the value I want to set to client
c := newCredentialClient(&result{})

// Here I want to set client value as c.
// But got error: cannot use c (variable of type *credentialClient) as reflect.Value value in argument to // client.Set
client.Set(c)

Is there have any way can let me set this client value as I expected? And I can make sure this client can be set since client.canSet() value is true. thanks in advance.





Aucun commentaire:

Enregistrer un commentaire