Skip to content

Commit

Permalink
Updated README for surrealdb go driver (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbshah97 authored Nov 20, 2023
1 parent 9f3a289 commit 57a4a91
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The official SurrealDB library for Golang.

## Getting Started

For instructions on how to follow SurrealDB, follow [Installation Guide](https://surrealdb.com/docs/installation)

### Installation

```bash
Expand All @@ -21,33 +23,36 @@ go get github.com/surrealdb/surrealdb.go

```go
package main

import (
"fmt"
"github.com/surrealdb/surrealdb.go"
"github.com/surrealdb/surrealdb.go"

)

type User struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Surname string `json:"surname"`
}

func main() {
// Connect to SurrealDB
db, err := surrealdb.New("ws://localhost:8000/rpc")
if err != nil {
panic(err)
}

// Sign in
if _, err = db.Signin(map[string]string{
"user": "root",
"pass": "root",
}); err != nil {
panic(err)
}

// Select namespace and database
if _, err = db.Use("test", "test"); err != nil {
panic(err)
}

// Create user struct
// Define user struct
user := User{
Name: "John",
Surname: "Doe",
Expand Down Expand Up @@ -81,16 +86,12 @@ func main() {

// Change part/parts of user
changes := map[string]string{"name": "Jane"}
if _, err = db.Change(selectedUser.ID, changes); err != nil {
panic(err)
}

// Update user
if _, err = db.Update(selectedUser.ID, changes); err != nil {
panic(err)
}

// Raw Query user
if _, err = db.Query("SELECT * FROM $record", map[string]interface{}{
"record": createdUser[0].ID,
}); err != nil {
Expand All @@ -102,9 +103,13 @@ func main() {
panic(err)
}
}

```

* Step 1: Create a file called `main.go` and paste the above code
* Step 2: Run the command `go mod init github.com/<github-username>/<project-name>` to create a go.mod file
* Step 3: Run the command `go mod tidy` to download surreal db
* Step 4: Run `go run main.go` to run the application.

# Documentation

Full documentation is available at [surrealdb doc](https://surrealdb.com/docs/integration/libraries/golang)
Expand Down

0 comments on commit 57a4a91

Please sign in to comment.