i was creating a simple marshaler for fixed with text into struct in golang, as detailed here http://ift.tt/2uwcqod
The marshaler functions as I expected now, albeit still lacking some features. What I got stuck in is in the marshal function.
The relevant code is as follow
func Marshal(obj interface{}) (str string, err error) {
...
elemsType := reflect.TypeOf(obj).Elem()
As you can see, i tried to mimic the json package's marshal signature. Then only problem is when i tried to pass by value to the marshal function, the reflect.TypeOf returns a different type than the one i pass into it. The function can only executed if I'm passing pointer to the marshal function.
This works user := User{"johnjohnjohn", "the", "doe", "smart", 26} res, err := Marshal(&user)
This doesn't user := User{"johnjohnjohn", "the", "doe", "smart", 26} res, err := Marshal(user)
Is there any way to just pass by value, and then get the struct tag inside the marshal function?
Aucun commentaire:
Enregistrer un commentaire