Hello, I’m continuing my investigations into "Big Data’ and with this in mind have started to experiment with the denisenkom/go-mssqldb
Golang package. Because I’m getting connection problems, I thought I would try debugging. So I set up my launch.json
like this:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"remotePath": "",
"port": 2345,
"host": "127.0.0.1",
"program": "${fileDirname}",
"env": {},
"args": ["-server 192.168.99.100 -user sa -password <MyPassword> -port 1433"],
"showLog": true
}
]
}
I’m running SQL Server in a Docker container, but I know by connecting with MS SQL Operations Studio that 192.168.99.100 is the correct server address.
I’m trying to run the example code from the denisenkom/go-mssqldb
package which is as follows:
package main
import (
"database/sql"
"flag"
"fmt"
"log"
_ "github.com/denisenkom/go-mssqldb"
)
var (
debug = flag.Bool("debug", false, "enable debugging")
password = flag.String("password", "", "the database password")
port = flag.Int("port", 1433, "the database port")
server = flag.String("server", "", "the database server")
user = flag.String("user", "", "the database user")
)
func main() {
flag.Parse()
if *debug {
fmt.Printf(" password:%s\n", *password)
fmt.Printf(" port:%d\n", *port)
fmt.Printf(" server:%s\n", *server)
fmt.Printf(" user:%s\n", *user)
}
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
if *debug {
fmt.Printf(" connString:%s\n", connString)
}
conn, err := sql.Open("mssql", connString)
if err != nil {
log.Fatal("Open connection failed:", err.Error())
}
defer conn.Close()
stmt, err := conn.Prepare("select 1, 'abc'")
if err != nil {
log.Fatal("Prepare failed:", err.Error())
}
defer stmt.Close()
row := stmt.QueryRow()
var somenumber int64
var somechars string
err = row.Scan(&somenumber, &somechars)
if err != nil {
log.Fatal("Scan failed:", err.Error())
}
fmt.Printf("somenumber:%d\n", somenumber)
fmt.Printf("somechars:%s\n", somechars)
fmt.Printf("bye\n")
}
The problem seems to occur when the flag.Parse()
line is hit. The error message is flag provided but not defined: -server 192.168.99.100 -user sa -password <MyPassword> -port 1433
. A quick look on StackOverflow tells me:
flag.Parse() is being called before your flag is defined.
You have to make sure that all flag definitions happen before calling flag.Parse(), usually by defining all flags inside init() functions.
But I thought that the lines in the var block:
var (
debug = flag.Bool("debug", false, "enable debugging")
password = flag.String("password", "", "the database password")
port = flag.Int("port", 1433, "the database port")
server = flag.String("server", "", "the database server")
user = flag.String("user", "", "the database user")
)
constituted the necessary flag definition. Indeed the standard documentation has example code following the same pattern. What am I missing?