Ent
Ent is an easy-to-use ORM which developed by Facebook. Here is a brief guide to use this library.
Add two fields name、age
to User
table.
package schema
import (
"entgo.io/ent"
"entgo.io/ent/schema/field"
)
// Fields of the User.
func (User) Fields() []ent.Field {
field.Int("age").
field.String("name").
Default("unknown"),
}
}
First, create a new ent.Client
. We use SQLite3 here for demonstration.
package main
import (
"context"
"log"
"<project>/ent"
)
func main() {
client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
if err != nil {
log.Fatalf("failed opening connection to sqlite: %v", err)
}
defer client.Close()
// Run the auto migration tool.
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
}