Skip to content

aholstenson/loro-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

loro-go

Release Go Reference Go Version License

This repository contains Loro CRDT bindings for Go. Contains pre-built binaries for MacOS (ARM64, AMD64), Linux (ARM64, AMD64), and Windows (AMD64).

Current Loro version: 1.12.0

Note: The API is pre-1.0 and may change.

Usage

go get github.com/aholstenson/loro-go

You need CGO enabled to build, but you do not need a Rust toolchain - the pre-built static libraries are included in this module.

On Linux you will likely want to statically link your binary to avoid a runtime dependency on libgcc:

go build -ldflags '-linkmode external -extldflags "-static"'

Examples

Getting started

doc := loro.NewLoroDoc()

m := doc.GetMap(loro.AsContainerId("settings"))
m.InsertAny("theme", "dark")

theme, ok := m.GetString("theme")

Nested containers

m := doc.GetMap(loro.AsContainerId("doc"))

users, err := m.GetOrCreateLoroMap("users")
alice, err := users.GetOrCreateLoroMap("alice")
alice.InsertAny("name", "Alice")

Collaborative text

note := doc.GetText(loro.AsContainerId("note"))
note.Insert(0, "Hello, world!")
note.Insert(7, "Loro ")
// note.String() == "Hello, Loro world!"

Syncing two documents

a := loro.NewLoroDoc()
b := loro.NewLoroDoc()

// Send everything b is missing from a.
updates, err := a.Export(loro.UpdatesMode(b.StateVv()))
status, err := b.Import(updates)

// For a fresh peer, send a full snapshot instead:
snapshot, err := a.Export(loro.SnapshotMode())

Merging concurrent edits

Two docs can edit independently and converge after exchanging updates:

a := loro.NewLoroDoc()
b := loro.NewLoroDoc()

a.GetMap(loro.AsContainerId("m")).InsertAny("from-a", int64(1))
b.GetMap(loro.AsContainerId("m")).InsertAny("from-b", int64(2))

aUpdates, _ := a.Export(loro.UpdatesMode(b.StateVv()))
bUpdates, _ := b.Export(loro.UpdatesMode(a.StateVv()))

b.Import(aUpdates)
a.Import(bUpdates)

// Both docs now contain from-a and from-b.

Updating the Loro version

Ensure you use --recursive when you git clone this repository to pull in the loro-ffi submodule.

./scripts/updateLoro.sh 1.x.x

You can get the version number from the loro-ffi tags page.

This updates the loro-ffi submodule, loro-go/Cargo.toml, and the lockfile. Open a PR with the changes and CI will build the libraries and commit them after merge.

About

Go bindings for Loro CRDT

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages