Integrate Spanner with GORM (GoogleSQL dialect)

GORM is an object-relational mapping tool for the Go programming language. It provides a framework for mapping an object-oriented domain model to a relational database.

You can integrate GoogleSQL-dialect databases with GORM using the open source Spanner Dialect ( SpannerDialect ).

Set up GORM with Spanner GoogleSQL-dialect databases

To use the GoogleSQL GORM dialect in your application, add the following import statement to the file where GORM is initialized:

  import 
 ( 
 "fmt" 
 "gorm.io/gorm" 
 _ 
 "github.com/googleapis/go-sql-spanner" 
 spannergorm 
 "github.com/googleapis/go-gorm-spanner" 
 ) 
 dsn 
 := 
 fmt 
 . 
 Sprintf 
 ( 
 "projects/ 
 %s 
 /instances/ 
 %s 
 /databases/ 
 %s 
 " 
 , 
 projectId 
 , 
 instanceId 
 , 
 databaseId 
 ), 
 db 
 , 
 err 
 := 
 gorm 
 . 
 Open 
 ( 
 spannergorm 
 . 
 New 
 ( 
 spannergorm 
 . 
 Config 
 { 
 DriverName 
 : 
 "spanner" 
 , 
 DSN 
 : 
 dsn 
 }), 
& gorm 
 . 
 Config 
 {}) 
 

See the GORM with GoogleSQL documentation for more connection options for Spanner.

Use GORM with Spanner GoogleSQL-dialect databases

For more information about the features and recommendations for using GORM with Spanner, consult the reference documentation on GitHub.

What's next