Skip to content

Commit cdb1902

Browse files
committed
Connectors: Go sample
1 parent a4852a8 commit cdb1902

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
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,10 @@
1+
module example/hello
2+
3+
go 1.22.3
4+
5+
require (
6+
github.com/tarantool/go-iproto v1.0.0 // indirect
7+
github.com/tarantool/go-tarantool/v2 v2.1.0 // indirect
8+
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
9+
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
10+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
3+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4+
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5+
github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLhdVjw=
6+
github.com/tarantool/go-iproto v1.0.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo=
7+
github.com/tarantool/go-tarantool/v2 v2.1.0 h1:IY33WoS8Kqb+TxNnKbzu/7yVkiCNZGhbG5Gw0/tMfSk=
8+
github.com/tarantool/go-tarantool/v2 v2.1.0/go.mod h1:cpjGW5FHAXIMf0PKZte70pMOeadw1MA/hrDv1LblWk4=
9+
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU=
10+
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
11+
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
12+
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
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,105 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/tarantool/go-tarantool/v2"
7+
"time"
8+
)
9+
10+
func main() {
11+
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
12+
defer cancel()
13+
dialer := tarantool.NetDialer{
14+
Address: "127.0.0.1:3301",
15+
User: "sampleuser",
16+
Password: "123456",
17+
}
18+
opts := tarantool.Opts{
19+
Timeout: time.Second,
20+
}
21+
22+
conn, err := tarantool.Connect(ctx, dialer, opts)
23+
if err != nil {
24+
fmt.Println("Connection refused:", err)
25+
return
26+
}
27+
28+
// Interacting with the database
29+
// Insert data
30+
tuple1, err := conn.Do(
31+
tarantool.NewInsertRequest("bands").
32+
Tuple([]interface{}{1, "Roxette", 1986}),
33+
).Get()
34+
tuple2, err := conn.Do(
35+
tarantool.NewInsertRequest("bands").
36+
Tuple([]interface{}{2, "Scorpions", 1965}),
37+
).Get()
38+
tuple3, err := conn.Do(
39+
tarantool.NewInsertRequest("bands").
40+
Tuple([]interface{}{3, "Ace of Base", 1987}),
41+
).Get()
42+
tuple4, err := conn.Do(
43+
tarantool.NewInsertRequest("bands").
44+
Tuple([]interface{}{4, "The Beatles", 1960}),
45+
).Get()
46+
fmt.Println("Inserted tuples: ", tuple1, tuple2, tuple3, tuple4)
47+
48+
// Select by primary key
49+
data, err := conn.Do(
50+
tarantool.NewSelectRequest("bands").
51+
Limit(10).
52+
Iterator(tarantool.IterEq).
53+
Key([]interface{}{uint(1)}),
54+
).Get()
55+
fmt.Println("Tuple selected the primary key value:", data)
56+
57+
// Select by secondary key
58+
data, err = conn.Do(
59+
tarantool.NewSelectRequest("bands").
60+
Index("band").
61+
Limit(10).
62+
Iterator(tarantool.IterEq).
63+
Key([]interface{}{"The Beatles"}),
64+
).Get()
65+
fmt.Println("Tuple selected the secondary key value:", data)
66+
67+
// Update
68+
data, err = conn.Do(
69+
tarantool.NewUpdateRequest("bands").
70+
Key(tarantool.IntKey{2}).
71+
Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")),
72+
).Get()
73+
fmt.Println("Updated tuple:", data)
74+
75+
// Upsert
76+
data, err = conn.Do(
77+
tarantool.NewUpsertRequest("bands").
78+
Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}).
79+
Operations(tarantool.NewOperations().Assign(1, "The Doors")),
80+
).Get()
81+
82+
// Replace
83+
data, err = conn.Do(
84+
tarantool.NewReplaceRequest("bands").
85+
Tuple([]interface{}{1, "Queen", 1970}),
86+
).Get()
87+
fmt.Println("Replaced tuple:", data)
88+
89+
// Delete
90+
data, err = conn.Do(
91+
tarantool.NewDeleteRequest("bands").
92+
Key([]interface{}{uint(5)}),
93+
).Get()
94+
fmt.Println("Deleted tuple:", data)
95+
96+
// Call
97+
data, err = conn.Do(
98+
tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}),
99+
).Get()
100+
fmt.Println("Stored procedure result:", data)
101+
102+
// Close connection
103+
conn.CloseGraceful()
104+
fmt.Println("Connection is closed")
105+
}

0 commit comments

Comments
 (0)