go - Get struct tag without pointer -


i creating simple marshaler fixed text struct in go, detailed here.

the marshaler functions expected now, albeit still lacking features. got stuck in in marshal function.

the relevant code follow

func marshal(obj interface{}) (str string, err error) { ...     elemstype := reflect.typeof(obj).elem() 

as can see, tried mimic json package's marshal signature. problem when tried pass value marshal function, reflect.typeof returns different type 1 pass it. function can executed if i'm passing pointer 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 way pass value, , struct tag inside marshal function?

if want work on values, don't call type.elem() on reflect type. handle both (pointers , non-pointers), check if of pointer type, , call type.elem():

elemstype := reflect.typeof(obj) if elemstype.kind() == reflect.ptr {     elemstype = elemstype.elem() } 

Comments

Popular posts from this blog

html - How to set bootstrap input responsive width? -

javascript - Highchart x and y axes data from json -

javascript - Get js console.log as python variable in QWebView pyqt -