Skip to content

Update the Get Started tutorial for Go connector #4193

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

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
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
14 changes: 14 additions & 0 deletions doc/code_snippets/snippets/connectors/go/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Go

A sample application containing requests from the [Connecting from Go](https://www.tarantool.io/en/doc/latest/how-to/getting_started_go) tutorial.


## Running

Before running this sample, start an application that allows remote connections to a sample database: [sample_db](../instances.enabled/sample_db).

Then, start this sample by executing the following command in the `go` directory:

```
$ go run .
```
12 changes: 12 additions & 0 deletions doc/code_snippets/snippets/connectors/go/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module example/hello

go 1.22.3

require (
github.com/google/uuid v1.3.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/tarantool/go-iproto v1.0.0 // indirect
github.com/tarantool/go-tarantool/v2 v2.1.0 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
)
18 changes: 18 additions & 0 deletions doc/code_snippets/snippets/connectors/go/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLhdVjw=
github.com/tarantool/go-iproto v1.0.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
github.com/tarantool/go-tarantool/v2 v2.1.0 h1:IY33WoS8Kqb+TxNnKbzu/7yVkiCNZGhbG5Gw0/tMfSk=
github.com/tarantool/go-tarantool/v2 v2.1.0/go.mod h1:cpjGW5FHAXIMf0PKZte70pMOeadw1MA/hrDv1LblWk4=
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
134 changes: 134 additions & 0 deletions doc/code_snippets/snippets/connectors/go/hello.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package main

import (
"context"
"fmt"
"github.com/tarantool/go-tarantool/v2"
_ "github.com/tarantool/go-tarantool/v2/datetime"
_ "github.com/tarantool/go-tarantool/v2/decimal"
_ "github.com/tarantool/go-tarantool/v2/uuid"
"time"
)

func main() {
// Connect to the database
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: you can add a comment // Connect to the database to make this source file more readable even without the doc page.

defer cancel()
dialer := tarantool.NetDialer{
Address: "127.0.0.1:3301",
User: "sampleuser",
Password: "123456",
}
opts := tarantool.Opts{
Timeout: time.Second,
}

conn, err := tarantool.Connect(ctx, dialer, opts)
if err != nil {
fmt.Println("Connection refused:", err)
return
}

// Interact with the database
// ...
// Insert data
tuples := [][]interface{}{
{1, "Roxette", 1986},
{2, "Scorpions", 1965},
{3, "Ace of Base", 1987},
{4, "The Beatles", 1960},
}
var futures []*tarantool.Future
for _, tuple := range tuples {
request := tarantool.NewInsertRequest("bands").Tuple(tuple)
futures = append(futures, conn.Do(request))
}
fmt.Println("Inserted tuples:")
for _, future := range futures {
result, err := future.Get()
if err != nil {
fmt.Println("Got an error:", err)
} else {
fmt.Println(result)
}
}

// Select by primary key
data, err := conn.Do(
tarantool.NewSelectRequest("bands").
Limit(10).
Iterator(tarantool.IterEq).
Key([]interface{}{uint(1)}),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}
fmt.Println("Tuple selected by the primary key value:", data)

// Select by secondary key
data, err = conn.Do(
tarantool.NewSelectRequest("bands").
Index("band").
Limit(10).
Iterator(tarantool.IterEq).
Key([]interface{}{"The Beatles"}),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}
fmt.Println("Tuple selected by the secondary key value:", data)

// Update
data, err = conn.Do(
tarantool.NewUpdateRequest("bands").
Key(tarantool.IntKey{2}).
Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}
fmt.Println("Updated tuple:", data)

// Upsert
data, err = conn.Do(
tarantool.NewUpsertRequest("bands").
Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}).
Operations(tarantool.NewOperations().Assign(1, "The Doors")),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}

// Replace
data, err = conn.Do(
tarantool.NewReplaceRequest("bands").
Tuple([]interface{}{1, "Queen", 1970}),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}
fmt.Println("Replaced tuple:", data)

// Delete
data, err = conn.Do(
tarantool.NewDeleteRequest("bands").
Key([]interface{}{uint(5)}),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}
fmt.Println("Deleted tuple:", data)

// Call
data, err = conn.Do(
tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}),
).Get()
if err != nil {
fmt.Println("Got an error:", err)
}
fmt.Println("Stored procedure result:", data)

// Close connection
conn.CloseGraceful()
fmt.Println("Connection is closed")
}
Loading