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.

    1. package schema
    2. import (
    3. "entgo.io/ent"
    4. "entgo.io/ent/schema/field"
    5. )
    6. // Fields of the User.
    7. func (User) Fields() []ent.Field {
    8. field.Int("age").
    9. field.String("name").
    10. Default("unknown"),
    11. }
    12. }

    First, create a new ent.Client. We use SQLite3 here for demonstration.

    1. package main
    2. import (
    3. "context"
    4. "log"
    5. "<project>/ent"
    6. )
    7. func main() {
    8. client, err := ent.Open("sqlite3", "file:ent?mode=memory&cache=shared&_fk=1")
    9. if err != nil {
    10. log.Fatalf("failed opening connection to sqlite: %v", err)
    11. }
    12. defer client.Close()
    13. // Run the auto migration tool.
    14. if err := client.Schema.Create(context.Background()); err != nil {
    15. log.Fatalf("failed creating schema resources: %v", err)
    16. }
    17. }