Skip to content

Commit ddcc010

Browse files
committed
readme: copy walking-through example to tests
It can be useful to have a simple example with go-connector usage with detailed comments. Walking-through example is a single example that left in a README. The goal of example is to be an entrypoint for new users and to be an initial snippet suitable for playing with Go connector. Add a description with steps how-to run example manually.
1 parent 03f0960 commit ddcc010

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ faster than other packages according to public benchmarks.
2121
* [Installation](#installation)
2222
* [Hello World](#hello-world)
2323
* [API reference](#api-reference)
24-
* [Walking\-through example in Go](#walking-through-example-in-go)
24+
* [Walking\-through example](#walking-through-example)
2525
* [Help](#help)
2626
* [Usage](#usage)
2727
* [Schema](#schema)
@@ -108,7 +108,7 @@ The source file for error-handling tools is
108108
which has structure definitions and constants whose names are equivalent to names
109109
of errors that the Tarantool server returns.
110110

111-
## Walking-through example in Go
111+
## Walking-through example
112112

113113
We can now have a closer look at the `example.go` program and make some observations
114114
about what it does.

example_walk_through_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Run Tarantool instance before example execution:
2+
// Terminal 1:
3+
// $ TT_LISTEN=3013 TT_WORK_DIR=$(mktemp -d -t 'tarantool.XXX') tarantool config.lua
4+
//
5+
// Terminal 2:
6+
// $ go test -v example_test.go
7+
package tarantool_test
8+
9+
// The line "`github.com/tarantool/go-tarantool`" in the
10+
// `import(...)` section brings in all Tarantool-related functions and
11+
// structures.
12+
import (
13+
"fmt"
14+
"github.com/tarantool/go-tarantool"
15+
)
16+
17+
func Example_walkThrough() {
18+
// The line beginning with "Opts :=" sets up the options for
19+
// Connect(). In this example, there is only one thing in the structure, a user
20+
// name. The structure can also contain:
21+
// - Pass (password),
22+
// - Timeout (maximum number of milliseconds to wait before giving up),
23+
// - Reconnect (number of seconds to wait before retrying if a
24+
// connection fails),
25+
// - MaxReconnect (maximum number of times to retry).
26+
opts := tarantool.Opts{User: "guest"}
27+
// The line containing "tarantool.Connect" is essential for
28+
// beginning any session. There are two parameters:
29+
// - a string with host:port format or path to a UNIX socket, and
30+
// - the optional structure that was set up earlier.
31+
// The err structure will be nil if there is no error,
32+
// otherwise it will have a description which can be retrieved with err.Error().
33+
conn, err := tarantool.Connect("127.0.0.1:3301", opts)
34+
if err != nil {
35+
fmt.Println("Connection refused:", err)
36+
}
37+
// The Insert request, like almost all requests, is preceded by
38+
// "conn." which is the name of the object that was returned by Connect().
39+
// There are two parameters:
40+
// - a space number (it could just as easily have been a space name), and
41+
// - a tuple.
42+
resp, err := conn.Insert(999, []interface{}{99999, "BB"})
43+
if err != nil {
44+
fmt.Println("Error", err)
45+
fmt.Println("Code", resp.Code)
46+
}
47+
}

0 commit comments

Comments
 (0)