Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated README for surrealdb go driver #111

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
db, err := surrealdb.New("ws://localhost:8000/rpc")
if err != nil {
panic(err)
}

// Sign in
if _, err = db.Signin(map[string]string{
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
"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 {
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved
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)
}
}
sbshah97 marked this conversation as resolved.
Show resolved Hide resolved

```

* 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