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

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -