title | summary | toc | referral_id | docs_area | |
---|---|---|---|---|---|
Build a Go App with CockroachDB and GORM |
Learn how to use CockroachDB from a simple Go application with the GORM ORM. |
true |
false |
docs_go_gorm |
get_started |
{% include {{ page.version.version }}/filter-tabs/crud-go.md %}
This tutorial shows you how build a simple CRUD Go application with CockroachDB and the GORM ORM.
{{site.data.alerts.callout_success}}
For another use of GORM with CockroachDB, see our examples-orms
repository.
{{site.data.alerts.end}}
{% include {{ page.version.version }}/setup/sample-setup.md %}
Clone the code's GitHub repo:
{% include_cached copy-clipboard.html %}
$ git clone https://github.com/cockroachlabs/example-app-go-gorm
The project has the following directory structure:
├── README.md
└── main.go
The main.go
file defines an Account
struct that maps to a new accounts
table. The file also contains some read and write database operations that are executed in the main
method of the program.
{% include_cached copy-clipboard.html %}
{% remote_include https://raw.githubusercontent.com/cockroachlabs/example-app-go-gorm/master/main.go %}
{{site.data.alerts.callout_info}}
CockroachDB may require the [client to retry a transaction]({% link {{ page.version.version }}/transactions.md %}#transaction-retries) in the case of read/write [contention]({% link {{ page.version.version }}/performance-best-practices-overview.md %}#transaction-contention). The CockroachDB Go client includes a generic retry function (ExecuteTx()
) that runs inside a transaction and retries it as needed. The code sample shows how you can use this function to wrap SQL statements.
{{site.data.alerts.end}}
-
Navigate to the
example-app-go-gorm
directory:{% include_cached copy-clipboard.html %}
$ cd example-app-go-gorm
-
Set the
DATABASE_URL
environment variable to the connection string for your cluster:{% include_cached copy-clipboard.html %}
export DATABASE_URL="postgresql://root@localhost:26257/defaultdb?sslmode=disable"
{% include_cached copy-clipboard.html %}
export DATABASE_URL="{connection-string}"
Where
{connection-string}
is the connection string you copied earlier.
-
Initialize the module:
{% include_cached copy-clipboard.html %}
$ cd example-app-go-gorm
{% include_cached copy-clipboard.html %}
$ go mod init basic-sample && go mod tidy
-
Run the code:
{% include_cached copy-clipboard.html %}
$ go run main.go
The output should look similar to the following:
2021/09/16 14:17:12 Creating 5 new accounts... 2021/09/16 14:17:12 Accounts created. Balance at '2021-09-16 14:17:12.68843 -0400 EDT m=+2.760587790': 1580d2f4-c9ec-4f26-bbe7-6a53e9aa5170 1947 26ddc77b-8068-409b-b305-0c5d873f7c43 7987 3d97ea5a-5108-4388-88e8-92524d5de5e8 4159 af49831d-d637-4a20-a9a7-01e9fe4628fe 8181 f0cc97ef-e3fe-4abb-a44a-0dd04207f7d4 2181 2021/09/16 14:17:12 Transferring 100 from account af49831d-d637-4a20-a9a7-01e9fe4628fe to account 3d97ea5a-5108-4388-88e8-92524d5de5e8... 2021/09/16 14:17:12 Funds transferred. Balance at '2021-09-16 14:17:12.759686 -0400 EDT m=+2.831841311': 1580d2f4-c9ec-4f26-bbe7-6a53e9aa5170 1947 26ddc77b-8068-409b-b305-0c5d873f7c43 7987 3d97ea5a-5108-4388-88e8-92524d5de5e8 4259 af49831d-d637-4a20-a9a7-01e9fe4628fe 8081 f0cc97ef-e3fe-4abb-a44a-0dd04207f7d4 2181 2021/09/16 14:17:12 Deleting accounts created... 2021/09/16 14:17:12 Accounts deleted.
The code runs a migration that creates the
accounts
table in thebank
database, based on theAccount
struct defined at the top of themain.go
file.As shown in the output, the code also does the following:
- Inserts some rows into the
accounts
table. - Reads values from the table.
- Updates values in the table.
- Deletes values from the table.
- Inserts some rows into the
Read more about using the GORM ORM, or check out a more realistic implementation of GORM with CockroachDB in our examples-orms
repository.
{% include {{ page.version.version }}/app/see-also-links.md %}