Giter Site home page Giter Site logo

dataindataout / yugabyte-simple-go-app Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yugabytedb-samples/yugabyte-simple-go-app

1.0 1.0 0.0 218 KB

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

License: Apache License 2.0

Go 100.00%
yugabyte yugabytedb

yugabyte-simple-go-app's Introduction

Simple Go Application for YugabyteDB

The application connects to your YugabyteDB instance via Go PostgreSQL driver pgx and performs basic SQL operations. The instructions below are provided for local deployments Yugabyte Docs for single node or Yugabyte Docs for local multi-node. If you use a different type of deployment, then update the sample-app.go file with proper connection parameters.

This example was adapted from the pq example.

Prerequisites

  • Go version 1.19.4 or later is preferred. Earlier versions should work as well.
  • Command line tool or your favorite IDE, such as VSCode.

Clone Application Repository

Clone the application to your machine:

git clone https://github.com/dataindataout/yugabyte-simple-go-app.git && cd yugabyte-simple-go-app

Provide Cluster Connection Parameters

Open the main.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 Managed deployments.
  • sslRootCert - a full path to your CA root cert if used (for example, /Users/ybme/certificates/root.crt)

Build and Run Application

  1. Import the required packages:

    go mod tidy
  2. Run the application:

    go run main.go

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

>>>> 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 main.go:

  1. main function - establishes a connection with your cloud instance via the Go PostgreSQL driver pgx.
  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, or raise a question on StackOverflow and tag the question with yugabytedb!

yugabyte-simple-go-app's People

Contributors

dataindataout avatar ddhodge avatar dmagda avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.