Skip to content

Latest commit

 

History

History
132 lines (97 loc) · 5.17 KB

build-a-go-app-with-cockroachdb-gorm.md

File metadata and controls

132 lines (97 loc) · 5.17 KB
title summary toc twitter referral_id
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_hello_world_go_gorm

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}}

Step 1. Start CockroachDB

{% include {{ page.version.version }}/app/sample-setup.md %}

Step 2. Get the code

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 in the bank database. 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 in the case of read/write 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}}

Step 3. Run the code

  1. Initialize the module:

    {% include_cached copy-clipboard.html %}

    $ go mod init basic-sample && go mod tidy
  2. Run the code:

    {% include_cached copy-clipboard.html %}

    $ go run main.go

    The program will prompt you for a connection string to the database:

    Enter a connection string:
    
  3. Enter the connection string to your running cluster.

    {{site.data.alerts.callout_success}} postgresql://root@localhost:26257?sslmode=disable should be the sql connection URL provided in the cockroach welcome text. {{site.data.alerts.end}}

    {{site.data.alerts.callout_success}} Use the connection string provided in the Connection info window of the {{ site.data.products.db }} Console. {{site.data.alerts.end}}

    {{site.data.alerts.callout_info}} You need to provide a SQL user password in order to securely connect to a {{ site.data.products.db }} cluster. The connection string should have a placeholder for the password (<ENTER-PASSWORD>). {{site.data.alerts.end}}

    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 the bank database, based on the Account struct defined at the top of the main.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.

What's next?

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 %}