This guide is intended for first-time users who want instructions on how to set up an Ent project from scratch. Before we get started, make sure you have the following prerequisites installed on your machine.
After installing these dependencies, create a directory for the project and initialize a Go module:
Run the following Go commands to install Ent, and tell it to initialize the project structure along with a
After installing Ent and running
ent init, your project directory should look like this:
ent directory holds the generated assets (see the next section), and the
ent/schema directory contains your
When we ran
ent init Todo above, a schema named
Todo was created in the
todo.go file under the
As you can see, initially, the schema has no fields or edges defined. Let's run the command for generating assets to interact with
go generate ./ent invoked Ent's automatic code generation tool, which uses the schemas we define in our
schema package to generate the actual Go code which we will now use to interact with a database. At this stage, you can find under
./ent/client.go, client code that is capable of querying and mutating the
Todo entities. Let's create a
testable example to use this. We'll use SQLite
in this test-case for testing Ent.
Paste the following code in
example_test.go that instantiates an
ent.Client and automatically creates all schema resources
in the database (tables, columns, etc).
go test to verify that everything works as expected.
After setting up our project, we're ready to create our Todo list.