This example shows how to use the ObjectBox's Go API to create a simple console-based task-list application.
# 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
.
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
.
Files:
- main.go: actual example code
- model/task.go: Task struct declaration
- Other files in the model directory are generated by the objectbox-gogen bindings generator utility.
- The
main()
sets up ObjectBox store and auto-generatedTasksBox
which simplifies working with Tasks inside ObjectBox initObjectBox()
is the method that actually does ObjectBox setup based on the model. Note the unique IDs for the lastEntity and the call toRegisterBinding
- they make sure the database schema is up-to-date with the code.- The
runInteractiveShell()
is just a command executor and doesn't contain any ObjectBox related code. It displays the shell and blocks until anexit
command is entered. - After the exit command, execution of the program is back in the
main()
and objectbox is closed properly. Note that thebox
is closed beforeob
(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 (callsbox.GetAll()
) and displays tasks in a tablecreateTask()
- insert (callsbox.Put()
)setDone()
- selectbox.Get()
& updatebox.Put()
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:
- Edit
task.go
- 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
. - 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.