Can I edit/add struct tag at runtime?

7,729 views
Skip to first unread message

Nguyên Nguyễn Văn Cao

unread,
Oct 16, 2012, 12:38:57 PM10/16/12
to golan...@googlegroups.com
Can I edit/add struct tag at runtime?
I'm writting a simple wrap-pkg for appengine/datastore and labix.org/v2/mgo. mgo support for dropping index by a method, since datastore use struct tag.
If I can't edit struct tag at runtime, what is the alternative methods?

Jan Mercl

unread,
Nov 5, 2012, 4:20:12 AM11/5/12
to t...@recursivedream.com, golang-nuts
On Mon, Nov 5, 2012 at 5:07 AM, <t...@recursivedream.com> wrote:
> I was actually wondering something similar. In general, can structs be
> modified or created at runtime? Similarly to Nguyen, I am working with the
> appengine/datastore package and would like to be able to save entities
> without necessarily knowing the Kind ahead of time (Kind is a struct). If I
> get all the properties and the Kind name and so forth, I'd like to simply
> define it when necessary. Or something similar, anyway.

Go is a 99% static and 1% dynamic language. How would be doing
anything sane with run time defined struct types implemented? Using
string names for accessing the fields? If so, and I would not
recommend that approach, then you can write such type / library
package now, using what Go offers today with e.g. maps and "unsafe".
But then again, maybe Javascript is actually better suited for such,
by nature dynamic-types-involved tasks.

-j

Tom Davis

unread,
Nov 5, 2012, 1:04:46 PM11/5/12
to Jan Mercl, golang-nuts
Jan,

It's not that I thought it would be a particularly *sane* idea, I just
wanted to know if it was possible (and idiomatic, to some degree). I
didn't imagine this was the case but being new to Go it helps to find
the boundaries of the language—and get reacquainted with the general
boundaries of static languages, having used exclusively dynamic
languages for the past decade.

This sort of thing informs my usage of third-party APIs, as well as how
I construct my own. Knowing the limits of reflection and runtime
behavior and the expected use cases for an API, I can better decide what
sorts of arguments to have (and what to look for in others' APIs, as
well). For instance, perhaps knowing some degree of flexibility was in
order, the AppEngine Go team allows Entities to be struct pointers *or*
anything that implements a particular interface. By contrast, the Python
team assumes you will use the supplied `Model` subclass which makes many
assumptions and has many private APIs, but is more dynamic in terms of
ad-hoc definition and runtime specification modification.

Go is a beautifully simple and succinct language; most of its
capabilities are quite obvious. I'm just trying to round out my
knowledge by testing the edges of its reflection and other "1%
capabilities". I do appreciate you response :)


Cheers,

Tom

>
> -j

mish...@gmail.com

unread,
Apr 27, 2014, 7:32:09 PM4/27/14
to golan...@googlegroups.com, Jan Mercl, t...@recursivedream.com
Hi, someone has the solution?

sergey....@gmail.com

unread,
Mar 2, 2017, 4:22:31 AM3/2/17
to golang-nuts
Yes, you can. You can use this library: github.com/sevlyar/retag .

вторник, 16 октября 2012 г., 23:38:57 UTC+7 пользователь nvcnvn написал:
Message has been deleted

Konstantin Khomoutov

unread,
Mar 2, 2017, 7:09:45 AM3/2/17
to golang-nuts, 0xj...@gmail.com, t...@recursivedream.com
Hi, someone has the solution?
[...]

Note that since 1.8 Go ignores tags on type conversions [1]
so if you're actually dealing with an "XY problem" [2] -- trying
to monkey-patch a struct type's tags at runtime to make it
behave differently under different circumstances when
(un)marshaling values of it -- you might consider defining two
almost identical types which differ only in field tags¹ and use
type conversions of values when (un)marshaling them in
different ways.

¹ This can be done using code generation for instance;
   see `go help generate`.

marts...@gmail.com

unread,
Mar 2, 2017, 8:54:41 AM3/2/17
to golang-nuts
Reply all
Reply to author
Forward
0 new messages