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/
> 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
http://golang.org/doc/go_spec.html#Struct_types
-rob
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
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
Besides the other answers, you may find it helpful to look at
http://research.swtch.com/2009/12/data-structures-go-programs.html .
Ian