How to Use ent (Facebook's ORM) for Go Database Access

Define Go schemas, run entc generate, and use the generated client to perform type-safe database operations.

Use ent by defining your schema in Go structs, generating the code with entc, and then using the generated client to query your database.

// 1. Define schema in schema/user.go
type User struct {
    ent.Schema
}

func (User) Fields() []ent.Field {
    return []ent.Field{
        field.String("name"),
    }
}

// 2. Generate code: entc generate
// 3. Use in main.go
client, _ := ent.Open("sqlite3", "file:ent?mode=memory")
user, _ := client.User.Create().SetName("Alice").Save(ctx)
_ = user.Name