Skip to content

Latest commit

 

History

History

client-go

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Sync Client Example: using the Go SDK in a command line application

This example shows how to use the ObjectBox's Go API to create a simple console-based task-list application.

Setup

# Enter this directory
cd client-go

# Download dependencies
go mod tidy

# Download the ObjectBox C library
bash <(curl -s https://raw.githubusercontent.com/objectbox/objectbox-go/main/install.sh) --sync

# Build the example
export LD_LIBRARY_PATH=lib/:$LD_LIBRARY_PATH
go build main.go

# Run the example
./main

On MacOS, you may have to replace LD_LIBRARY_PATH with DYLD_LIBRARY_PATH.

Usage

The application supports the following commands:

Welcome to the ObjectBox tasks-list app example
Available commands are: 
    ls [-a]        list tasks - unfinished or all (-a flag)
    new Task text  create a new task with the text 'Task text'
    done ID        mark task with the given ID as done
    exit           close the program
    help           display this help
$ 

To create a new task, you can just type new Buy milk and hit enter.

Now you can display the list of open tasks by running ls:

 ID  Created                        Finished                       Text
  1  2018-11-14 14:36:57 +0100 CET                                 Buy milk

To complete a task, you would execute command done 1 (1 is the ID of the task as shown above). And to show all tasks, including the finished ones, you can run ls -a.

Code overview

Files:

Code organization

  1. The main() sets up ObjectBox store and auto-generated TasksBox which simplifies working with Tasks inside ObjectBox
  2. initObjectBox() is the method that actually does ObjectBox setup based on the model. Note the unique IDs for the lastEntity and the call to RegisterBinding - they make sure the database schema is up-to-date with the code.
  3. The runInteractiveShell() is just a command executor and doesn't contain any ObjectBox related code. It displays the shell and blocks until an exit command is entered.
  4. After the exit command, execution of the program is back in the main() and objectbox is closed properly. Note that the box is closed before ob (ObjectBox) by the design of GO defer statement (last-in-first-out).

The following methods demonstrate some of the API capabilities:

  • printList() - iterates over the whole dataset (calls box.GetAll()) and displays tasks in a table
  • createTask() - insert (calls box.Put())
  • setDone() - select box.Get() & update box.Put()

Changing the data model

When the model is changed, you need to run go generate inside the model folder so that the generated bindings are updated. For convenience, the auto-generated files task.obx.go and objectbox-model.json are already generated for this example.

You may want to change the data model to expend the example or to start your own data model for a new app. Here's what you have to do:

  1. Edit task.go
  2. Regenerate bindings and update model-info using go generate. For the go generate command to work, you need to have objectbox-gogen, a command line utility we have built for this purpose, installed and recognized in $PATH.
  3. Once the bindings are changed, you might need to adjust your initObjectBox() if you have added an entity - register bindings for completely new entities and update ID/UID

Some quick notes on IDs and UIDs in the ObjectBox model:

  • Entity IDs need to be unique among themselves
  • Property IDs need to be unique inside their Entity
  • All UIDs need to be unique application-wide
  • See Meta Model, IDs, and UIDs docs for more details.

Documentation