Go DriverArangoDB 官方 Go 驅(qū)動(dòng)程序
Go Driver 是 ArangoDB 數(shù)據(jù)庫(kù)的官方 Go 驅(qū)動(dòng)程序。
支持的版本
- ArangoDB 3.1 及更高版本
- 單服務(wù)器和集群設(shè)置
- 有或沒(méi)有認(rèn)證都支持
- Go 1.7 及更高版本
Go 依賴
- 無(wú)
配置
要使用驅(qū)動(dòng)程序,首先將源提取到您的GOPATH.
go get github.com/arangodb/go-driver
使用驅(qū)動(dòng)程序,需要始終創(chuàng)建一個(gè)Client. 以下示例顯示如何在 localhost 上運(yùn)行的單個(gè)服務(wù)器創(chuàng)建一個(gè) Client。
import ( "fmt" driver "github.com/arangodb/go-driver" "github.com/arangodb/go-driver/http" ) ... conn, err := http.NewConnection(http.ConnectionConfig{ Endpoints: []string{"http://localhost:8529"}, }) if err != nil { // Handle error } client, err := driver.NewClient(driver.ClientConfig{ Connection: conn, }) if err != nil { // Handle error }
創(chuàng)建Client后,可以在服務(wù)器上訪問(wèn)/創(chuàng)建的數(shù)據(jù)庫(kù),訪問(wèn)/創(chuàng)建集合、圖形、文檔等。
重要類型
使用 Go 驅(qū)動(dòng)程序需要了解的關(guān)鍵類型是:
-
Database -
Collection -
Graph EdgeDefinition
連接到 ArangoDB
conn, err := http.NewConnection(http.ConnectionConfig{ Endpoints: []string{"http://localhost:8529"}, TLSConfig: &tls.Config{ /*...*/ }, }) if err != nil { // Handle error } c, err := driver.NewClient(driver.ClientConfig{ Connection: conn, Authentication: driver.BasicAuthentication("user", "password"), }) if err != nil { // Handle error }
打開(kāi)數(shù)據(jù)庫(kù)
ctx := context.Background() db, err := client.Database(ctx, "myDB") if err != nil { // handle error }
檢查集合是否存在
ctx := context.Background() found, err := db.CollectionExists(ctx, "myCollection") if err != nil { // handle error }
創(chuàng)建集合
ctx := context.Background() options := &driver.CreateCollectionOptions{ /* ... */ } col, err := db.CreateCollection(ctx, "myCollection", options) if err != nil { // handle error }
評(píng)論
圖片
表情
