Im coming back to golang after a couple of years and I've forgotten how go modules are supposed to work...
This is my main file - sync.go
package main
import (
"database/sql"
"fmt"
"os"
_ "github.com/jackc/pgx/v4/stdlib"
)
func main() {
// urlExample := "postgres://username:password@localhost:5432/database_name"
db, err := sql.Open("pgx", "postgres://localhost:5432/my_db")
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
defer db.Close()
var result string
err = db.QueryRow("select distinct name from my_table limit 1").Scan(&result)
if err != nil {
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
os.Exit(1)
}
fmt.Println(result)
}
go.mod
:
Have added the pgx lib by doing go get "github.com/jackc/pgx/v4"
module sync
go 1.17
require (
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.11.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.2.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
github.com/jackc/pgtype v1.10.0 // indirect
github.com/jackc/pgx/v4 v4.15.0 // indirect
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
golang.org/x/text v0.3.6 // indirect
)
go-sql on master [+?] via v1.17
❯ ls
go.mod go.sum sync.go vendor
go-sql on master [+?] via v1.17
❯ cat go.mod
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
│ File: go.mod
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
1 │ module sync
2 │
3 │ go 1.17
4 │
5 │ require (
6 │ github.com/jackc/chunkreader/v2 v2.0.1 // indirect
7 │ github.com/jackc/pgconn v1.11.0 // indirect
8 │ github.com/jackc/pgio v1.0.0 // indirect
9 │ github.com/jackc/pgpassfile v1.0.0 // indirect
10 │ github.com/jackc/pgproto3/v2 v2.2.0 // indirect
11 │ github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b // indirect
12 │ github.com/jackc/pgtype v1.10.0 // indirect
13 │ github.com/jackc/pgx/v4 v4.15.0 // indirect
14 │ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect
15 │ golang.org/x/text v0.3.6 // indirect
16 │ )
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
go-sql on master [+?] via v1.17
❯ go mod vendor
go: warning: "all" matched no packages
go-sql on master [+?] via v1.17
❯ ls vendor
modules.txt
Why is my vendor directory empty ? What am I missing ?
If I try to compile, it fails as expected as my vendor directory is empty -
❯ go run sync.go
sync.go:8:2: cannot find package "." in:
/home/ninan/trials/go-sql/vendor/github.com/jackc/pgx/v4/stdlib