Skip to content

Commit 19f4600

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

File tree

5 files changed

+82
-32
lines changed

5 files changed

+82
-32
lines changed

doc/code_snippets/snippets/connectors/go/go.mod

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ module example/hello
33
go 1.22.3
44

55
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
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
1012
)

doc/code_snippets/snippets/connectors/go/go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
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=
24
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=
37
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
48
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
59
github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLhdVjw=

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

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import (
44
"context"
55
"fmt"
66
"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"
710
"time"
811
)
912

@@ -27,23 +30,26 @@ func main() {
2730

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

4854
// Select by primary key
4955
data, err := conn.Do(
@@ -52,6 +58,9 @@ func main() {
5258
Iterator(tarantool.IterEq).
5359
Key([]interface{}{uint(1)}),
5460
).Get()
61+
if err != nil {
62+
fmt.Println("Got an error:", err)
63+
}
5564
fmt.Println("Tuple selected the primary key value:", data)
5665

5766
// Select by secondary key
@@ -62,6 +71,9 @@ func main() {
6271
Iterator(tarantool.IterEq).
6372
Key([]interface{}{"The Beatles"}),
6473
).Get()
74+
if err != nil {
75+
fmt.Println("Got an error:", err)
76+
}
6577
fmt.Println("Tuple selected the secondary key value:", data)
6678

6779
// Update
@@ -70,6 +82,9 @@ func main() {
7082
Key(tarantool.IntKey{2}).
7183
Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")),
7284
).Get()
85+
if err != nil {
86+
fmt.Println("Got an error:", err)
87+
}
7388
fmt.Println("Updated tuple:", data)
7489

7590
// Upsert
@@ -78,25 +93,37 @@ func main() {
7893
Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}).
7994
Operations(tarantool.NewOperations().Assign(1, "The Doors")),
8095
).Get()
96+
if err != nil {
97+
fmt.Println("Got an error:", err)
98+
}
8199

82100
// Replace
83101
data, err = conn.Do(
84102
tarantool.NewReplaceRequest("bands").
85103
Tuple([]interface{}{1, "Queen", 1970}),
86104
).Get()
105+
if err != nil {
106+
fmt.Println("Got an error:", err)
107+
}
87108
fmt.Println("Replaced tuple:", data)
88109

89110
// Delete
90111
data, err = conn.Do(
91112
tarantool.NewDeleteRequest("bands").
92113
Key([]interface{}{uint(5)}),
93114
).Get()
115+
if err != nil {
116+
fmt.Println("Got an error:", err)
117+
}
94118
fmt.Println("Deleted tuple:", data)
95119

96120
// Call
97121
data, err = conn.Do(
98122
tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}),
99123
).Get()
124+
if err != nil {
125+
fmt.Println("Got an error:", err)
126+
}
100127
fmt.Println("Stored procedure result:", data)
101128

102129
// Close connection

doc/how-to/getting_started_go.rst

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ Now you can create a client Go application that makes requests to this database.
3838
Developing a client application
3939
-------------------------------
4040

41+
Before you start, make sure you have `Go installed <https://go.dev/doc/install>`__ on your computer.
42+
4143
.. _getting_started_go_create_client_app:
4244

4345
Creating an application
@@ -61,8 +63,8 @@ Creating an application
6163

6264
.. _getting_started_go_import_:
6365

64-
Importing 'go-tarantool'
65-
~~~~~~~~~~~~~~~~~~~~~~~~
66+
Importing 'go-tarantool' packages
67+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6668

6769
In the ``hello.go`` file, declare a ``main`` package and import the following packages:
6870

@@ -72,11 +74,13 @@ In the ``hello.go`` file, declare a ``main`` package and import the following pa
7274
:end-before: func main()
7375
:dedent:
7476

77+
The packages for external MsgPack types, such as ``datetime``, ``decimal``, or ``uuid``, are required to parse these types in a response.
78+
7579

7680
.. _getting_started_go_creating_connection:
7781

78-
Creating a connection
79-
~~~~~~~~~~~~~~~~~~~~~
82+
Connecting to the database
83+
~~~~~~~~~~~~~~~~~~~~~~~~~~
8084

8185
1. Declare the ``main()`` function:
8286

@@ -114,11 +118,18 @@ Add the following code to insert four tuples into the ``bands`` space:
114118
.. literalinclude:: /code_snippets/snippets/connectors/go/hello.go
115119
:language: go
116120
:start-at: // Insert data
117-
:end-at: Inserted tuples:
121+
:end-before: // Select by primary key
118122
:dedent:
119123

120-
The ``NewInsertRequest()`` method creates an insert request object that is executed by the connection.
124+
This code makes insert requests asynchronously:
125+
126+
- The ``Future`` structure is used as a handle for asynchronous requests.
127+
- The ``NewInsertRequest()`` method creates an insert request object that is executed by the connection.
128+
129+
.. NOTE::
121130

131+
Making requests asynchronously is the recommended way to perform data operations.
132+
Further requests in this tutorial are made synchronously.
122133

123134

124135
.. _getting_started_go_querying_data:
@@ -157,7 +168,7 @@ Updating data
157168
:end-at: Updated tuple
158169
:dedent:
159170

160-
``NewUpsertRequest()`` can be used to update an existing tuple or inserts a new one.
171+
``NewUpsertRequest()`` can be used to update an existing tuple or insert a new one.
161172
In the example below, a new tuple is inserted:
162173

163174
.. literalinclude:: /code_snippets/snippets/connectors/go/hello.go
@@ -231,18 +242,24 @@ The ``CloseGraceful()`` method can be used to close the connection when it is no
231242
Starting a client application
232243
-----------------------------
233244

234-
1. Execute the ``go get`` command to update dependencies in the ``go.mod`` file:
245+
1. Execute the following ``go get`` commands to update dependencies in the ``go.mod`` file:
235246

236247
.. code-block:: console
237248
238249
$ go get github.com/tarantool/go-tarantool/v2
250+
$ go get github.com/tarantool/go-tarantool/v2/decimal
251+
$ go get github.com/tarantool/go-tarantool/v2/uuid
239252
240253
2. To run the resulting application, execute the ``go run`` command in the application directory:
241254

242255
.. code-block:: console
243256
244257
$ go run .
245-
Inserted tuples: [[1 Roxette 1986]] [[2 Scorpions 1965]] [[3 Ace of Base 1987]] [[4 The Beatles 1960]]
258+
Inserted tuples:
259+
[[1 Roxette 1986]]
260+
[[2 Scorpions 1965]]
261+
[[3 Ace of Base 1987]]
262+
[[4 The Beatles 1960]]
246263
Tuple selected the primary key value: [[1 Roxette 1986]]
247264
Tuple selected the secondary key value: [[4 The Beatles 1960]]
248265
Updated tuple: [[2 Pink Floyd 1965]]
@@ -258,7 +275,7 @@ Starting a client application
258275
Feature comparison
259276
------------------
260277

261-
There are two more connectors from the open source community:
278+
There are two more connectors from the open-source community:
262279

263280
* `viciious/go-tarantool <https://github.com/viciious/go-tarantool>`_
264281

doc/how-to/getting_started_net_box.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This section describes the :ref:`configuration <configuration_file>` of a sample
2222
:language: yaml
2323
:dedent:
2424

25-
- The configuration contains one instance that listens incoming requests on the ``127.0.0.1:3301`` address.
25+
- The configuration contains one instance that listens for incoming requests on the ``127.0.0.1:3301`` address.
2626
- ``sampleuser`` has :ref:`privileges <authentication-owners_privileges>` to select and modify data in the ``bands`` space and execute the ``get_bands_older_than`` stored function. This user can be used to connect to the instance remotely.
2727
- ``myapp.lua`` defines how data is stored in a database and includes a stored function.
2828

0 commit comments

Comments
 (0)