-
Notifications
You must be signed in to change notification settings - Fork 43
Update the Get Started tutorial for Go connector #4193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# Go | ||
|
||
A sample application containing requests from the [Connecting from Go](https://www.tarantool.io/en/doc/latest/how-to/getting_started_go) tutorial. | ||
|
||
|
||
## Running | ||
|
||
Before running this sample, start an application that allows remote connections to a sample database: [sample_db](../instances.enabled/sample_db). | ||
|
||
Then, start this sample by executing the following command in the `go` directory: | ||
|
||
``` | ||
$ go run . | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module example/hello | ||
|
||
go 1.22.3 | ||
|
||
require ( | ||
github.com/google/uuid v1.3.0 // indirect | ||
github.com/shopspring/decimal v1.3.1 // indirect | ||
github.com/tarantool/go-iproto v1.0.0 // indirect | ||
github.com/tarantool/go-tarantool/v2 v2.1.0 // indirect | ||
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect | ||
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= | ||
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= | ||
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= | ||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= | ||
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8= | ||
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o= | ||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= | ||
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= | ||
github.com/tarantool/go-iproto v1.0.0 h1:quC4hdFhCuFYaCqOFgUxH2foRkhAy+TlEy7gQLhdVjw= | ||
github.com/tarantool/go-iproto v1.0.0/go.mod h1:LNCtdyZxojUed8SbOiYHoc3v9NvaZTB7p96hUySMlIo= | ||
github.com/tarantool/go-tarantool/v2 v2.1.0 h1:IY33WoS8Kqb+TxNnKbzu/7yVkiCNZGhbG5Gw0/tMfSk= | ||
github.com/tarantool/go-tarantool/v2 v2.1.0/go.mod h1:cpjGW5FHAXIMf0PKZte70pMOeadw1MA/hrDv1LblWk4= | ||
github.com/vmihailenco/msgpack/v5 v5.3.5 h1:5gO0H1iULLWGhs2H5tbAHIZTV8/cYafcFOr9znI5mJU= | ||
github.com/vmihailenco/msgpack/v5 v5.3.5/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc= | ||
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g= | ||
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds= | ||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/tarantool/go-tarantool/v2" | ||
_ "github.com/tarantool/go-tarantool/v2/datetime" | ||
_ "github.com/tarantool/go-tarantool/v2/decimal" | ||
_ "github.com/tarantool/go-tarantool/v2/uuid" | ||
"time" | ||
) | ||
|
||
func main() { | ||
// Connect to the database | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: you can add a comment |
||
defer cancel() | ||
dialer := tarantool.NetDialer{ | ||
Address: "127.0.0.1:3301", | ||
User: "sampleuser", | ||
Password: "123456", | ||
} | ||
opts := tarantool.Opts{ | ||
Timeout: time.Second, | ||
} | ||
|
||
conn, err := tarantool.Connect(ctx, dialer, opts) | ||
if err != nil { | ||
fmt.Println("Connection refused:", err) | ||
return | ||
} | ||
|
||
// Interact with the database | ||
// ... | ||
// Insert data | ||
tuples := [][]interface{}{ | ||
{1, "Roxette", 1986}, | ||
{2, "Scorpions", 1965}, | ||
{3, "Ace of Base", 1987}, | ||
{4, "The Beatles", 1960}, | ||
} | ||
var futures []*tarantool.Future | ||
for _, tuple := range tuples { | ||
request := tarantool.NewInsertRequest("bands").Tuple(tuple) | ||
futures = append(futures, conn.Do(request)) | ||
} | ||
fmt.Println("Inserted tuples:") | ||
for _, future := range futures { | ||
result, err := future.Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} else { | ||
fmt.Println(result) | ||
} | ||
} | ||
|
||
// Select by primary key | ||
data, err := conn.Do( | ||
tarantool.NewSelectRequest("bands"). | ||
Limit(10). | ||
Iterator(tarantool.IterEq). | ||
Key([]interface{}{uint(1)}), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
fmt.Println("Tuple selected by the primary key value:", data) | ||
|
||
// Select by secondary key | ||
data, err = conn.Do( | ||
tarantool.NewSelectRequest("bands"). | ||
Index("band"). | ||
Limit(10). | ||
Iterator(tarantool.IterEq). | ||
Key([]interface{}{"The Beatles"}), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
fmt.Println("Tuple selected by the secondary key value:", data) | ||
|
||
// Update | ||
data, err = conn.Do( | ||
tarantool.NewUpdateRequest("bands"). | ||
Key(tarantool.IntKey{2}). | ||
Operations(tarantool.NewOperations().Assign(1, "Pink Floyd")), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
fmt.Println("Updated tuple:", data) | ||
|
||
// Upsert | ||
data, err = conn.Do( | ||
tarantool.NewUpsertRequest("bands"). | ||
Tuple([]interface{}{uint(5), "The Rolling Stones", 1962}). | ||
Operations(tarantool.NewOperations().Assign(1, "The Doors")), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
|
||
// Replace | ||
data, err = conn.Do( | ||
tarantool.NewReplaceRequest("bands"). | ||
Tuple([]interface{}{1, "Queen", 1970}), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
fmt.Println("Replaced tuple:", data) | ||
|
||
// Delete | ||
data, err = conn.Do( | ||
tarantool.NewDeleteRequest("bands"). | ||
Key([]interface{}{uint(5)}), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
fmt.Println("Deleted tuple:", data) | ||
|
||
// Call | ||
data, err = conn.Do( | ||
tarantool.NewCallRequest("get_bands_older_than").Args([]interface{}{1966}), | ||
).Get() | ||
if err != nil { | ||
fmt.Println("Got an error:", err) | ||
} | ||
fmt.Println("Stored procedure result:", data) | ||
|
||
// Close connection | ||
conn.CloseGraceful() | ||
fmt.Println("Connection is closed") | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.