github.com/cayleygraph/cayley@v0.7.7/docs/quickstart-as-lib.md (about)

     1  # Quickstart as Library
     2  
     3  Currently, Cayley supports being used as a Go library for other projects. To use it in such a way, here's a quick example:
     4  
     5  ```go
     6  package main
     7  
     8  import (
     9      "fmt"
    10      "log"
    11  
    12      "github.com/cayleygraph/cayley"
    13      "github.com/cayleygraph/quad"
    14  )
    15  
    16  func main() {
    17      // Create a brand new graph
    18      store, err := cayley.NewMemoryGraph()
    19      if err != nil {
    20          log.Fatalln(err)
    21      }
    22  
    23      store.AddQuad(quad.Make("phrase of the day", "is of course", "Hello World!", nil))
    24  
    25      // Now we create the path, to get to our data
    26      p := cayley.StartPath(store, quad.String("phrase of the day")).Out(quad.String("is of course"))
    27  
    28      // Now we iterate over results. Arguments:
    29      // 1. Optional context used for cancellation.
    30      // 2. Flag to optimize query before execution.
    31      // 3. Quad store, but we can omit it because we have already built path with it.
    32      err = p.Iterate(nil).EachValue(nil, func(value quad.Value){
    33          nativeValue := quad.NativeOf(value) // this converts RDF values to normal Go types
    34          fmt.Println(nativeValue)
    35      })
    36      if err != nil {
    37          log.Fatalln(err)
    38      }
    39  }
    40  ```
    41  
    42  To use other backends, you can empty-import them, eg
    43  
    44  ```go
    45  import _ "github.com/cayleygraph/cayley/graph/kv/bolt"
    46  ```
    47  
    48  And use them with a call like
    49  
    50  ```go
    51  import "github.com/cayleygraph/cayley/graph"
    52  
    53  func open() {
    54    // Initialize the database
    55    graph.InitQuadStore("bolt", path, nil)
    56  
    57    // Open and use the database
    58    cayley.NewGraph("bolt", path, nil)
    59  }
    60  ```
    61  
    62  More runnable examples are available in [examples](https://github.com/cayleygraph/cayley/tree/87c9c341848b59924a054ebc2dd0f2bf8c57c6a9/examples/README.md) folder.
    63