|
| 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