Annotations in Go (...to port JPA to Go?)

3,122 views
Skip to first unread message

Alexander Orlov

unread,
May 31, 2011, 8:17:40 AM5/31/11
to golang-nuts
I know, Generics is a hot topic too but that's not what I'm interested
in...

Currently there are no annotations in Go like they are available since
Java 5. Thanks to annotations you can write declarative code which is
heavily used to describe relational data structures like it's done in
JPA.

Are there plans to implement annotations and is is desirable at all?
Or are there already any JPA-like approaches to access AND describe a
relational database model?

André Moraes

unread,
May 31, 2011, 8:22:42 AM5/31/11
to golang-nuts
Maybe using simple structs and some reflection will be better than
implement something like JPA.

This will not have all de facilities of JPA when comes to mapping one
struct to many different tables.
But for the comon case: one struct <=> one table this looks better and
will not require any change in the language.

--
André Moraes
http://andredevchannel.blogspot.com/

Ian Lance Taylor

unread,
May 31, 2011, 1:45:55 PM5/31/11
to Alexander Orlov, golang-nuts
Alexander Orlov <alexand...@loxal.net> writes:

> Currently there are no annotations in Go like they are available since
> Java 5. Thanks to annotations you can write declarative code which is
> heavily used to describe relational data structures like it's done in
> JPA.
>
> Are there plans to implement annotations and is is desirable at all?
> Or are there already any JPA-like approaches to access AND describe a
> relational database model?

There are no current plans to implement anything like Java annotations.

Note that you can already put tags on struct fields. I'm not familiar
with JPA so I don't know if that is relevant.

Ian

mattn

unread,
May 31, 2011, 8:47:51 PM5/31/11
to golan...@googlegroups.com
I guess that annotation have advantage only talking about "//export" in cgo. :)

Alexander Orlov

unread,
Jun 1, 2011, 6:48:19 AM6/1/11
to golang-nuts
On May 31, 2:22 pm, André Moraes <andr...@gmail.com> wrote:
> Maybe using simple structs and some reflection will be better than
> implement something like JPA.
>
> This will not have all de facilities of JPA when comes to mapping one
> struct to many different tables.
> But for the comon case: one struct <=> one table this looks better and
> will not require any change in the language.

Based on my info from http://code.google.com/p/go/issues/detail?id=1704
about tags, those tags thing sounds pretty interesting. Unfortunately
I haven't found anything in "Effective Go" about tags only some pieces
in the Go Spec... Btw, where can I find a better doc/explanation about
tags?

Alexander Orlov

unread,
Jun 1, 2011, 8:03:01 AM6/1/11
to Ian Lance Taylor, golang-nuts

Rob 'Commander' Pike

unread,
Jun 1, 2011, 9:03:47 AM6/1/11
to Alexander Orlov, Ian Lance Taylor, golang-nuts
Tags in Go are nothing like Java annotations. See the last bit of

http://golang.org/doc/go_spec.html#Struct_types

-rob

Ibrahim M. Ghazal

unread,
Jun 1, 2011, 9:27:22 AM6/1/11
to Alexander Orlov, golan...@googlegroups.com
On Jun 1, 3:03 pm, Alexander Orlov <alexander.or...@loxal.net> wrote:
> On May 31, 2:22 pm, André Moraes <andr...@gmail.com> wrote:
>
> > Maybe using simple structs and some reflection will be better than
> > implement something like JPA.
>
> > This will not have all de facilities of JPA when comes to mapping one
> > struct to many different tables.
> > But for the comon case: one struct <=> one table this looks better and
> > will not require any change in the language.
>
> Based on my info fromhttp://code.google.com/p/go/issues/detail?id=1704

> about tags, those tags thing sounds pretty interesting. Unfortunately
> I haven't found anything in "Effective Go" about tags only some pieces
> in the Go Spec... Btw, where can I find a better doc/explanation about
> tags?

Tags are very simple, there is nothing to explain more than what the
spec says[1]:
<snip>
A field declaration [in a struct declaration] may be followed by an
optional string literal tag, which becomes an attribute for all the
fields in the corresponding field declaration. The tags are made
visible through a reflection interface but are otherwise ignored.

// A struct corresponding to the TimeStamp protocol buffer.
// The tag strings define the protocol buffer field numbers.
struct {
microsec uint64 "field 1"
serverIP6 uint64 "field 2"
process string "field 3"
}
</snip>

In the above example, "field 1", "field 2", and "field 3" are tags.

For an example of using tags in real code, see json.Marshal[2], its
documentation says:
<snip>
Struct values encode as JSON objects. Each struct field becomes a
member of the object. By default the object's key name is the struct
field name. If the struct field has a non-empty tag consisting of only
Unicode letters, digits, and underscores, that tag will be used as the
name instead. Only exported fields will be encoded.
</snip>
As an example, the following code:
package main

import (
"fmt"
"json"
)

type notags struct {Foo int}
type withtags struct {Foo int "my_tag"}

func main() {
j1, _ := json.Marshal(notags{1})
fmt.Println(string(j1))
j2, _ := json.Marshal(withtags{1})
fmt.Println(string(j2))
}

prints:
{"Foo":1}
{"my_tag":1}

The json package reads the tag ("my_tag") in withtags and uses it
instead of the field name ("Foo").

[1] http://golang.org/doc/go_spec.html#Struct_types
[2] http://golang.org/pkg/json/#Marshal

Alexander Orlov

unread,
Jun 1, 2011, 9:34:03 AM6/1/11
to Ibrahim M. Ghazal, golan...@googlegroups.com
On Wed, Jun 1, 2011 at 3:27 PM, Ibrahim M. Ghazal <img...@gmail.com> wrote:
As an example, the following code:
package main

import (
       "fmt"
       "json"
)

type notags struct {Foo int}
type withtags struct {Foo int "my_tag"}

func main() {
       j1, _ := json.Marshal(notags{1})
       fmt.Println(string(j1))
       j2, _ := json.Marshal(withtags{1})
       fmt.Println(string(j2))
}

prints:
{"Foo":1}
{"my_tag":1}

The json package reads the tag ("my_tag") in withtags and uses it
instead of the field name ("Foo").

[1] http://golang.org/doc/go_spec.html#Struct_types
[2] http://golang.org/pkg/json/#Marshal

Thx! I've thought there might be more to know.

-Alex

Ian Lance Taylor

unread,
Jun 1, 2011, 10:57:16 AM6/1/11
to Alexander Orlov, golang-nuts
Alexander Orlov <alexand...@loxal.net> writes:

Besides the other answers, you may find it helpful to look at
http://research.swtch.com/2009/12/data-structures-go-programs.html .

Ian

Reply all
Reply to author
Forward
0 new messages