Go SDK guide¶
This guide introduces how to set up the Go SDK to connect to NebulaGraph and perform simple queries.
Prerequisites¶
The Go SDK installation package and NebulaGraph connection information are obtained from NebulaGraph Cloud. For more information, see Connect to your database.
Steps¶
-
Modify the Go module dependencies to use the local version of the Go SDK. Example:
workspace=/app/myapp tar zxvf nebula-golang-5.0.0.tar.gz -o /tmp/golang # In your project, run go mod edit cd ${workspace} go mod edit -replace github.com/vesoft-inc/nebula-ng-tools/golang=/tmp/golang
For more information, see go.mod file reference.
-
Use the SDK to connect to NebulaGraph and perform queries as follows:
package main import ( "fmt" nebula "github.com/vesoft-inc/nebula-ng-tools/golang" ) const ( address = "<host>:<port>" username = "<username>" password = "<password>" ) // Initialize logger var log = nebula.DefaultLogger func basicClient() { client, err := nebula.NewNebulaClient(address, username, password) if err != nil { log.Error(err.Error()) return } resp, err := client.Execute("return 1 as a") if err != nil { log.Error(err.Error()) return } log.Info(fmt.Sprintf("columns: %v", resp.Columns())) for resp.HasNext() { row, err := resp.Next() if err != nil { log.Error(err.Error()) return } v1, err := row.GetValueByIndex(0) if err != nil { log.Error(err.Error()) return } log.Info(v1.String()) v2, err := row.GetValueByName("a") if err != nil { log.Error(err.Error()) return } log.Info(v2.String()) } } func basicPool() { pool, err := nebula.NewNebulaPool(address, username, password) if err != nil { log.Error(err.Error()) return } client, err := pool.GetClient() if err != nil { log.Error(err.Error()) return } resp, err := client.Execute("return 1 as a") if err != nil { log.Error(err.Error()) return } log.Info(fmt.Sprintf("columns: %v", resp.Columns())) for resp.HasNext() { row, err := resp.Next() if err != nil { log.Error(err.Error()) return } v1, err := row.GetValueByIndex(0) if err != nil { log.Error(err.Error()) return } log.Info(v1.String()) v2, err := row.GetValueByName("a") if err != nil { log.Error(err.Error()) return } log.Info(v2.String()) } } func main() { basicClient() basicPool() }
Replace
<host>
,<port>
,<username>
, and<password>
with the actual connection information obtained from NebulaGraph Cloud.