package main import ( "github.com/microsoft/go-mssqldb/azuread" // _ "github.com/microsoft/go-mssqldb" "database/sql" "context" "log" "fmt" ) var db *sql.DB func main() { var err error // Create connection pool db, err = sql.Open(azuread.DriverName, "sqlserver://xxx.database.windows.net:1433?database=xxx&fedauth=ActiveDirectoryManagedIdentity&user id=client_id_of_user-assigned_managed_identity") if err != nil { log.Fatal("Error creating connection pool: ", err.Error()) } ctx := context.Background() err = db.PingContext(ctx) if err != nil { log.Fatal(err.Error()) } fmt.Printf("Connected!\n") // Read table count, err := ReadTable() if err != nil { log.Fatal("Error reading Table: ", err.Error()) } fmt.Printf("Read %d row(s) successfully.\n", count) // Call Hello function hello() } // ReadTable reads a record from a specific table func ReadTable() (int, error) { ctx := context.Background() // Check if database is alive. err := db.PingContext(ctx) if err != nil { return -1, err } tsql := fmt.Sprintf("SELECT count(*) AS PlayerCount FROM Players;") // Execute query rows, err := db.QueryContext(ctx, tsql) if err != nil { return -1, err } defer rows.Close() var count int // Iterate through the result set. for rows.Next() { var PlayerCount int // Get values from row. err := rows.Scan(&PlayerCount) if err != nil { return -1, err } fmt.Printf("Player Count: %d\n", PlayerCount) count++ } return count, nil } func hello() { fmt.Println("Hello World!") }