Initialize the driver

    Note

    The article is being updated.

    To connect to YDB, you need to specify the required and additional parameters that define the driver's behavior (learn more in Connecting to the YDB server).

    Below are examples of the code for connecting to YDB (driver creation) in different YDB SDKs.

    package main
    
    import (
      "context"
      "os"
    
      "github.com/ydb-platform/ydb-go-sdk/v3"
    )
    
    func main() {
      db, err := ydb.Open(ctx, "grpcs://localhost:2135/local")
      if err != nil {
          log.Fatal(err)
      }
      ...
    }
    
    Using a connector (recommended)
    package main
    
    import (
      "context"
      "database/sql"
      "os"
    
      "github.com/ydb-platform/ydb-go-sdk/v3"
    )
    
    func main() {
      ctx, cancel := context.WithCancel(context.Background())
      defer cancel()
      nativeDriver, err := ydb.Open(ctx,
        os.Getenv("YDB_CONNECTION_STRING"),
      )
      if err != nil {
        panic(err)
      }
      defer nativeDriver.Close(ctx)
      connector, err := ydb.Connector(nativeDriver)
      if err != nil {
        panic(err)
      }
      db := sql.OpenDB(connector)
      defer db.Close()
      ...
    }
    
    Using a connection string

    The database/sql driver is registered when importing the package of a specific driver separated by an underscore:

    package main
    
    import (
      "context"
      "database/sql"
      "os"
    
      _ "github.com/ydb-platform/ydb-go-sdk/v3"
    )
    
    func main() {
      db, err := sql.Open("ydb", "grpcs://localhost:2135/local")
      if err != nil {
          log.Fatal(err)
      }
      ...
    }
    

    Note

    The article is being updated.