Skip to main content

Setting Up

Let's start by initializing a new Go module for our project:

mkdir ent-grpc-example
cd ent-grpc-example
go mod init ent-grpc-example

Next, we use go run to invoke the ent code generator to initialize a schema:

go run -mod=mod init User

Our directory should now look like:

โ”œโ”€โ”€ ent
โ”‚ โ”œโ”€โ”€ generate.go
โ”‚ โ””โ”€โ”€ schema
โ”‚ โ””โ”€โ”€ user.go
โ”œโ”€โ”€ go.mod
โ””โ”€โ”€ go.sum

Next, let's add the entproto package to our project:

go get -u

Next, we will define the schema for the User entity. Open ent/schema/user.go and edit:

package schema
import (
// User holds the schema definition for the User entity.
type User struct {
// Fields of the User.
func (User) Fields() []ent.Field {
return []ent.Field{

In this step, we added two unique fields to our User entity: name and email_address. The ent.Schema is just the definition of the schema. To create usable production code from it we need to run Ent's code generation tool on it. Run:

go generate ./...

Notice that new files were created from our schema definition:

โ”œโ”€โ”€ ent
โ”‚ โ”œโ”€โ”€ client.go
โ”‚ โ”œโ”€โ”€ config.go
// .... many more
โ”‚ โ”œโ”€โ”€ user
โ”‚ โ”œโ”€โ”€ user.go
โ”‚ โ”œโ”€โ”€ user_create.go
โ”‚ โ”œโ”€โ”€ user_delete.go
โ”‚ โ”œโ”€โ”€ user_query.go
โ”‚ โ””โ”€โ”€ user_update.go
โ”œโ”€โ”€ go.mod
โ””โ”€โ”€ go.sum

At this point, we can open a connection to a database, run a migration to create the users table, and start reading and writing data to it. This is covered on the Setup Tutorial, so let's cut to the chase and learn about generating Protobuf definitions and gRPC servers from our schema.