Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yugabytedb-samples/yugabyte-simple-go-app

A simple Go app that connects to a YugabyteDB database cluster and performs basic CRUD operations.
https://github.com/yugabytedb-samples/yugabyte-simple-go-app

cloud-native demos distributed-database distributed-sql go yugabyte yugabytedb

Last synced: about 7 hours ago
JSON representation

A simple Go app that connects to a YugabyteDB database cluster and performs basic CRUD operations.

Awesome Lists containing this project

README

        

# Simple Go Application for YugabyteDB

The application connects to your YugabyteDB instance via
[Go PostgreSQL driver](https://docs.yugabyte.com/latest/reference/drivers/ysql-client-drivers/#go-postgresql-driver-pq) and performs basic SQL
operations. The instructions below are provided for [YugabyteDB Aeon](https://cloud.yugabyte.com/) deployments.
If you use a different type of deployment, then update the `sample-app.go` file with proper connection parameters.

## Prerequisites

* Go version 1.17.6 or later is preffered. Earlier versions should work as well.
* Command line tool or your favourite IDE, such as Visual Studio Code.

## Start YugabyteDB Aeon Cluster

* [Start YugabyteDB Aeon](https://docs.yugabyte.com/latest/yugabyte-cloud/cloud-quickstart/qs-add/) instance. Free tier exists.
* Add an IP address of your machine/laptop to the [IP allow list](https://docs.yugabyte.com/latest/yugabyte-cloud/cloud-secure-clusters/add-connections/#manage-ip-allow-lists)

## Clone Application Repository

Clone the application to your machine:

```bash
git clone https://github.com/YugabyteDB-Samples/yugabyte-simple-go-app.git && cd yugabyte-simple-go-app
```

## Provide Cluster Connection Parameters

The application needs to establish a secured connection to your YugabyteDB Aeon instance.

Open the `sample-app.go` file and specify the following configuration parameters:
* `host` - the hostname of your instance.
* `port` - the port number of your instance (the default is `5433`).
* `dbUser` - the username for your instance.
* `dbPassword` - the database password.
* `sslMode` - the SSL mode. Set to `verify-full` for YugabyteDB Aeon deployments.
* `sslRootCert` - a full path to your CA root cert (for example, `/Users/dmagda/certificates/root.crt`)

Note, you can easily find all the settings on the YugabyteDB Aeon dashboard:

![image](resources/cloud_app_settings.png)

## Build and Run Application

1. Initialize the `GO111MODULE` variable:
```bash
export GO111MODULE=auto
```
2. Import the Go PostgreSQL driver:
```bash
go get github.com/lib/pq
```
3. Run the application:
```bash
go run sample-app.go
```

Upon successful execution, you will see output similar to the following:

```bash
>>>> Successfully connected to YugabyteDB!
>>>> Successfully created table DemoAccount.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 10000
name = John, age = 28, country = Canada, balance = 9000
>>>> Transferred 800 between accounts.
>>>> Selecting accounts:
name = Jessica, age = 28, country = USA, balance = 9200
name = John, age = 28, country = Canada, balance = 9800
```

## Explore App Logic

Congrats! You've successfully executed a simple Go app that works with YugabyteDB.

Now, explore the source code of `sample-app.go`:
1. `main` function - establishes a connection with your cloud instance via Go PostgreSQL driver.
2. `createDatabase` function - creates a table and populates it with sample data.
3. `selectAccounts` function - queries the data with SQL `SELECT` statements.
4. `transferMoneyBetweenAccounts` function - updates records consistently with distributed transactions.

## Questions or Issues?

Having issues running this application or want to learn more from Yugabyte experts?

Join [our Slack channel](https://communityinviter.com/apps/yugabyte-db/register),
or raise a question on StackOverflow and tag the question with `yugabytedb`!