Can anyone help me with this error please?
panic: dial tcp: lookup tcp/5432,: getaddrinfow: The specified class was not found.
goroutine 1 [running]:
main.main()
C:/Dev/Digitalent/scalable-web-service-with-golang/go-sql/main.go:35 +0x23a
exit status 2
this is the code.
package main
import (
"database/sql"
"fmt"
_ "github.com/lib/pq"
)
const (
host = "localhost"
port = 5432
username = "postgres"
password = "*********"
dbName = "db-go-sql"
)
var (
db *sql.DB
err error
)
func main() {
psqlURL := fmt.Sprintf("host=%s port=%d, user=%s, password=%s "+
"dbname=%s sslmode=disable", host, port, username, password, dbName)
db, err = sql.Open("postgres", psqlURL)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Println("Connection to postgresql established")
}
It's about connecting my Go Lang code with PostgreSQL. I have checked the connection over and over, such as host, port, username, password, and dbname, and it's all good I think. But, this error always came up when I run the main program. I'm using Windows 11. Thank you!
You do have a
,with the url string. Removing that comma should resolve your problem.This issue is originating from the lookupIP method in the
lookup_windows.gofile, when it look for a port as5432,