Skip to content

Commit 7be307e

Browse files
committed
Connectors: Go tutorial - update per review
1 parent 65c7637 commit 7be307e

File tree

2 files changed

+64
-19
lines changed

2 files changed

+64
-19
lines changed

doc/code_snippets/snippets/connectors/go/hello.go

+41-17
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,26 @@ func main() {
2727

2828
// Interacting with the database
2929
// 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)
30+
tuples := [][]interface{}{
31+
{1, "Roxette", 1986},
32+
{2, "Scorpions", 1965},
33+
{3, "Ace of Base", 1987},
34+
{4, "The Beatles", 1960},
35+
}
36+
var futures []*tarantool.Future
37+
for _, tuple := range tuples {
38+
request := tarantool.NewInsertRequest("bands").Tuple(tuple)
39+
futures = append(futures, conn.Do(request))
40+
}
41+
fmt.Println("Inserted tuples:")
42+
for _, future := range futures {
43+
result, err := future.Get()
44+
if err != nil {
45+
fmt.Println("Got an error:", err)
46+
} else {
47+
fmt.Println(result)
48+
}
49+
}
4750

4851
// Select by primary key
4952
data, err := conn.Do(
@@ -52,6 +55,9 @@ func main() {
5255
Iterator(tarantool.IterEq).
5356
Key([]interface{}{uint(1)}),
5457
).Get()
58+
if err != nil {
59+
fmt.Println("Got an error:", err)
60+
}
5561
fmt.Println("Tuple selected the primary key value:", data)
5662

5763
// Select by secondary key
@@ -62,6 +68,9 @@ func main() {
6268
Iterator(tarantool.IterEq).
6369
Key([]interface{}{"The Beatles"}),
6470
).Get()
71+
if err != nil {
72+
fmt.Println("Got an error:", err)
73+
}
6574
fmt.Println("Tuple selected the secondary key value:", data)
6675

6776
// Update
@@ -70,6 +79,9 @@ func main() {
7079
Key(tarantool.IntKey{2}).
7180
Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")),
7281
).Get()
82+
if err != nil {
83+
fmt.Println("Got an error:", err)
84+
}
7385
fmt.Println("Updated tuple:", data)
7486

7587
// Upsert
@@ -78,25 +90,37 @@ func main() {
7890
Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}).
7991
Operations(tarantool.NewOperations().Assign(1, "The Doors")),
8092
).Get()
93+
if err != nil {
94+
fmt.Println("Got an error:", err)
95+
}
8196

8297
// Replace
8398
data, err = conn.Do(
8499
tarantool.NewReplaceRequest("bands").
85100
Tuple([]interface{}{1, "Queen", 1970}),
86101
).Get()
102+
if err != nil {
103+
fmt.Println("Got an error:", err)
104+
}
87105
fmt.Println("Replaced tuple:", data)
88106

89107
// Delete
90108
data, err = conn.Do(
91109
tarantool.NewDeleteRequest("bands").
92110
Key([]interface{}{uint(5)}),
93111
).Get()
112+
if err != nil {
113+
fmt.Println("Got an error:", err)
114+
}
94115
fmt.Println("Deleted tuple:", data)
95116

96117
// Call
97118
data, err = conn.Do(
98119
tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}),
99120
).Get()
121+
if err != nil {
122+
fmt.Println("Got an error:", err)
123+
}
100124
fmt.Println("Stored procedure result:", data)
101125

102126
// Close connection

doc/how-to/getting_started_go.rst

+23-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ In the ``hello.go`` file, declare a ``main`` package and import the following pa
7272
:end-before: func main()
7373
:dedent:
7474

75+
.. NOTE::
76+
77+
To be able to parse external msgpack types, such as ``datetime``, ``decimal``, or ``uuid``, you need to import corresponding packages:
78+
79+
.. code-block:: go
80+
81+
import (
82+
"github.com/tarantool/go-tarantool/v2/datetime"
83+
"github.com/tarantool/go-tarantool/v2/decimal"
84+
"github.com/tarantool/go-tarantool/v2/uuid"
85+
)
86+
87+
These imports are not required for the purpose of this tutorial.
88+
7589

7690
.. _getting_started_go_creating_connection:
7791

@@ -114,11 +128,18 @@ Add the following code to insert four tuples into the ``bands`` space:
114128
.. literalinclude:: /code_snippets/snippets/connectors/go/hello.go
115129
:language: go
116130
:start-at: // Insert data
117-
:end-at: Inserted tuples:
131+
:end-before: // Select by primary key
118132
:dedent:
119133

120-
The ``NewInsertRequest()`` method creates an insert request object that is executed by the connection.
134+
This code makes insert requests asynchronously:
135+
136+
- The ``Future`` structure is used as a handle for asynchronous requests.
137+
- The ``NewInsertRequest()`` method creates an insert request object that is executed by the connection.
138+
139+
.. NOTE::
121140

141+
Making requests asynchronously is the recommended way to perform data operations.
142+
Further requests in this tutorial are made synchronously.
122143

123144

124145
.. _getting_started_go_querying_data:

0 commit comments

Comments
 (0)