go-gorm
Google Cloud Spanner ORM for
Go's GORM implementation.
import (
_ "github.com/googleapis/go-sql-spanner"
spannergorm "github.com/googleapis/go-gorm"
)
db, err := gorm.Open(spannergorm.New(spannergorm.Config{
DriverName: "spanner",
DSN: "projects/PROJECT/instances/INSTANCE/databases/DATABASE",
}), &gorm.Config{PrepareStmt: true})
if err != nil {
log.Fatal(err)
}
// Print tweets with more than 500 likes.
type Tweet struct {
ID int `gorm:"primarykey;autoIncrement:false"`
Text string
Likes int
}
var tweets []Tweet
if err := db.Where("likes > ?", 500).Find(&tweets).Error; err != nil {
log.Fatal(err)
}
for t := range tweets {
fmt.Println(t.ID, t.Text)
}Emulator
See the Google Cloud Spanner Emulator support to learn how to start the emulator.
Once the emulator is started and the host environmental flag is set, the driver should just work.
$ gcloud beta emulators spanner start
$ export SPANNER_EMULATOR_HOST=localhost:9010
Go Versions Supported
Our libraries are compatible with at least the three most recent, major Go
releases. They are currently compatible with:
- Go 1.20
- Go 1.19
- Go 1.18
- Go 1.17
Authorization
By default, each API will use Google Application Default Credentials
for authorization credentials used in calling the API endpoints. This will allow your
application to run in many environments without requiring explicit configuration.
Contributing
Contributions are welcome. Please, see the
CONTRIBUTING
document for details.
Please note that this project is released with a Contributor Code of Conduct.
By participating in this project you agree to abide by its terms.
See Contributor Code of Conduct
for more information.