Skip to content

Commit 24a959d

Browse files
Update the Get Started tutorial for Go connector (#4193)
1 parent 5d06f39 commit 24a959d

File tree

6 files changed

+366
-161
lines changed

6 files changed

+366
-161
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Go
2+
3+
A sample application containing requests from the [Connecting from Go](https://www.tarantool.io/en/doc/latest/how-to/getting_started_go) tutorial.
4+
5+
6+
## Running
7+
8+
Before running this sample, start an application that allows remote connections to a sample database: [sample_db](../instances.enabled/sample_db).
9+
10+
Then, start this sample by executing the following command in the `go` directory:
11+
12+
```
13+
$ go run .
14+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module example/hello
2+
3+
go 1.22.3
4+
5+
require (
6+
github.com/google/uuid v1.3.0 // indirect
7+
github.com/shopspring/decimal v1.3.1 // indirect
8+
github.com/tarantool/go-iproto v1.0.0 // indirect
9+
github.com/tarantool/go-tarantool/v2 v2.1.0 // indirect
10+
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
11+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
12+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
3+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
6+
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
7+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLhdVjw=
10+
github.com/tarantool/go-iproto v1.0.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
11+
github.com/tarantool/go-tarantool/v2 v2.1.0 h1:IY33WoS8Kqb+TxNnKbzu/7yVkiCNZGhbG5Gw0/tMfSk=
12+
github.com/tarantool/go-tarantool/v2 v2.1.0/go.mod h1:cpjGW5FHAXIMf0PKZte70pMOeadw1MA/hrDv1LblWk4=
13+
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
14+
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
15+
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
16+
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
17+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/tarantool/go-tarantool/v2"
7+
_ "github.com/tarantool/go-tarantool/v2/datetime"
8+
_ "github.com/tarantool/go-tarantool/v2/decimal"
9+
_ "github.com/tarantool/go-tarantool/v2/uuid"
10+
"time"
11+
)
12+
13+
func main() {
14+
// Connect to the database
15+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
16+
defer cancel()
17+
dialer := tarantool.NetDialer{
18+
Address: "127.0.0.1:3301",
19+
User: "sampleuser",
20+
Password: "123456",
21+
}
22+
opts := tarantool.Opts{
23+
Timeout: time.Second,
24+
}
25+
26+
conn, err := tarantool.Connect(ctx, dialer, opts)
27+
if err != nil {
28+
fmt.Println("Connection refused:", err)
29+
return
30+
}
31+
32+
// Interact with the database
33+
// ...
34+
// Insert data
35+
tuples := [][]interface{}{
36+
{1, "Roxette", 1986},
37+
{2, "Scorpions", 1965},
38+
{3, "Ace of Base", 1987},
39+
{4, "The Beatles", 1960},
40+
}
41+
var futures []*tarantool.Future
42+
for _, tuple := range tuples {
43+
request := tarantool.NewInsertRequest("bands").Tuple(tuple)
44+
futures = append(futures, conn.Do(request))
45+
}
46+
fmt.Println("Inserted tuples:")
47+
for _, future := range futures {
48+
result, err := future.Get()
49+
if err != nil {
50+
fmt.Println("Got an error:", err)
51+
} else {
52+
fmt.Println(result)
53+
}
54+
}
55+
56+
// Select by primary key
57+
data, err := conn.Do(
58+
tarantool.NewSelectRequest("bands").
59+
Limit(10).
60+
Iterator(tarantool.IterEq).
61+
Key([]interface{}{uint(1)}),
62+
).Get()
63+
if err != nil {
64+
fmt.Println("Got an error:", err)
65+
}
66+
fmt.Println("Tuple selected by the primary key value:", data)
67+
68+
// Select by secondary key
69+
data, err = conn.Do(
70+
tarantool.NewSelectRequest("bands").
71+
Index("band").
72+
Limit(10).
73+
Iterator(tarantool.IterEq).
74+
Key([]interface{}{"The Beatles"}),
75+
).Get()
76+
if err != nil {
77+
fmt.Println("Got an error:", err)
78+
}
79+
fmt.Println("Tuple selected by the secondary key value:", data)
80+
81+
// Update
82+
data, err = conn.Do(
83+
tarantool.NewUpdateRequest("bands").
84+
Key(tarantool.IntKey{2}).
85+
Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")),
86+
).Get()
87+
if err != nil {
88+
fmt.Println("Got an error:", err)
89+
}
90+
fmt.Println("Updated tuple:", data)
91+
92+
// Upsert
93+
data, err = conn.Do(
94+
tarantool.NewUpsertRequest("bands").
95+
Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}).
96+
Operations(tarantool.NewOperations().Assign(1, "The Doors")),
97+
).Get()
98+
if err != nil {
99+
fmt.Println("Got an error:", err)
100+
}
101+
102+
// Replace
103+
data, err = conn.Do(
104+
tarantool.NewReplaceRequest("bands").
105+
Tuple([]interface{}{1, "Queen", 1970}),
106+
).Get()
107+
if err != nil {
108+
fmt.Println("Got an error:", err)
109+
}
110+
fmt.Println("Replaced tuple:", data)
111+
112+
// Delete
113+
data, err = conn.Do(
114+
tarantool.NewDeleteRequest("bands").
115+
Key([]interface{}{uint(5)}),
116+
).Get()
117+
if err != nil {
118+
fmt.Println("Got an error:", err)
119+
}
120+
fmt.Println("Deleted tuple:", data)
121+
122+
// Call
123+
data, err = conn.Do(
124+
tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}),
125+
).Get()
126+
if err != nil {
127+
fmt.Println("Got an error:", err)
128+
}
129+
fmt.Println("Stored procedure result:", data)
130+
131+
// Close connection
132+
conn.CloseGraceful()
133+
fmt.Println("Connection is closed")
134+
}

0 commit comments

Comments
 (0)