Creating structus programmatically at runtime - possible?

4,424 views
Skip to first unread message

JF

unread,
Apr 8, 2013, 3:52:13 PM4/8/13
to golan...@googlegroups.com
Is it possible in Go to create a struct type programmatically (i.e. not in the compiled source code)?

We have a particular use case where a type will be created via user-defined metadata (so the schema / types are not known in advance)
and will vary for every customer. We would then need to auto-generate REST services for those and persist them in a NoSQL backend.
We would also need to define different dynamic validators per field (e.g. mandatory, regex, max/min size, max/min value, reference to another type instance, etc.)

In the Java world we could potentially use embedded compilers (like Janino) to create classes on demand, etc.
I was wondering if something similar is possible in the Go world?

e.g. define a struct as a String and compile it at runtime to an actual variable instance (or a type that could be instantiated via reflection).

Basically a Go version of what you can do in Python like this:
http://henry.precheur.org/python/Dynamically%20create%20a%20type%20with%20Python


Thanks

Péter Szilágyi

unread,
Apr 8, 2013, 4:08:50 PM4/8/13
to JF, golang-nuts
Hi,

  I'm not very familiar with the internals, but the Go gob package does something similar: it uses reflection during runtime to assemble serializers that convert structs into a wire format and on the other side back from wire format to Go struct (the deserializer is constructed based on the wire format only).  You could check the gob source code to see how they achieve this.

Cheers,
  Peter



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

minux

unread,
Apr 8, 2013, 4:12:06 PM4/8/13
to JF, golan...@googlegroups.com
On Tue, Apr 9, 2013 at 3:52 AM, JF <jac...@gmail.com> wrote:
> Is it possible in Go to create a struct type programmatically (i.e. not in
> the compiled source code)?
It's impossible, at least in the upcoming Go 1.1 and Go 1.0.x.

Scott Lawrence

unread,
Apr 8, 2013, 4:13:00 PM4/8/13
to JF, golan...@googlegroups.com
Two answer, depending on exactly what you want. (I wrote the second, then
realised I might have over-thought it.)

First answer: why aren't you just using maps? Why do you actually need a
proper 'struct'?

Second answer: Sorta.

Bear in mind that go is /not/ a dynamic language, so any code you write has to
compile. That means that any method you call has to actually exist, and be a
method of the type of the variable you're calling it on.

But what you should be able to do is have something like

type Happy interface {
Celebrate()
}

type DynamicHappy struct {
CelebrateFunc func()
}

func (dh DynamicHappy) Celebrate() {
dh.CelebrateFunc()
}

I don't think anything "more dynamic" than that would make sense within the
context of a statically compiled language. This gives you the ability to
construct arbitrary dynamic implementations of interfaces - I think that's the
best you can do. If you wanted to actually dynamically create an explicit
struct type, you wouldn't be able to use it anywhere, since none of the
(static) uses would compile!
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

--
Scott Lawrence

go version go1.0.3
Linux baidar 3.8.5-1-ARCH #1 SMP PREEMPT Fri Mar 29 19:18:14 CET 2013 x86_64 GNU/Linux

minux

unread,
Apr 8, 2013, 4:13:18 PM4/8/13
to Péter Szilágyi, JF, golang-nuts
On Tue, Apr 9, 2013 at 4:08 AM, Péter Szilágyi <pet...@gmail.com> wrote:
> I'm not very familiar with the internals, but the Go gob package does
> something similar: it uses reflection during runtime to assemble serializers
> that convert structs into a wire format and on the other side back from wire
> format to Go struct (the deserializer is constructed based on the wire
> format only). You could check the gob source code to see how they achieve
> this.
Gob requires you to register structs beforehand so that it doesn't need to
create a struct type dynamically (it's not possible).

Craig Weber

unread,
Apr 8, 2013, 4:54:10 PM4/8/13
to golan...@googlegroups.com, JF
I can't envision a use case in which dynamic structs would be more appropriate than maps...

JF

unread,
Apr 8, 2013, 5:01:53 PM4/8/13
to golan...@googlegroups.com, JF
It's because I was looking at some of the new orm libraries for Go (most of them are for SQL backends though)
and they all work off structs. So if I had a struct then I could piggy back on them for the actual backend storage.

Another option would be dynamic validators, I presumed if a library like that exists already it would probably rely on having a pre-defined struct with some validations defined on its fields, etc.

If I have to work with raw maps (which is probably the only solution anyway) then I will have to do all the low-level plumbing myself.

So I was looking at struct purely from a point of view of being able to reuse other libraries that will probably require at least a struct and won't work off a totally dynamic map.

Cheers
Reply all
Reply to author
Forward
0 new messages