forked from GoogleCloudPlatform/golang-samples
/
main.go
42 lines (33 loc) · 967 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// Copyright 2017 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Sample spanner_quickstart is a basic program that uses Cloud Spanner.
package main
import (
"fmt"
"log"
"cloud.google.com/go/spanner"
"golang.org/x/net/context"
)
func main() {
ctx := context.Background()
// This database must exist.
databaseName := "projects/your-project-id/instances/your-instance-id/databases/your-database-id"
client, err := spanner.NewClient(ctx, databaseName)
if err != nil {
log.Fatalf("Failed to create client %v", err)
}
defer client.Close()
stmt := spanner.Statement{SQL: "SELECT 1"}
iter := client.Single().Query(ctx, stmt)
defer iter.Stop()
row, err := iter.Next()
if err != nil {
log.Fatalf("Query failed with %v", err)
}
var i int64
if row.Columns(&i) != nil {
log.Fatalf("Failed to parse row %v", err)
}
fmt.Printf("Got value %v\n", i)
}