-1

I have this simple go file called main.go:

package main

import (
    "fmt"
    "database/sql"
    _ "github.com/mattn/go-sqlite3"
)

func main() {
    db, err := sql.Open("sqlite3", "mydb.db")

    if err != nil {
        panic(err)
    }

    defer db.Close()

    fmt.Println("Opened Successfully")
}

I downloaded the required package but when I try to go run . or go build . I get nothing, the compiler just seems to be stuck in an infinite loop. If I remove the line _ "github.com/mattn/go-sqlite3" it works as expected with the error message panic: sql: unknown driver "sqlit3" (forgotten import?).

this happens many times with some imported packages.

this is my go.mod file:

module go-sqlite

go 1.18

require github.com/mattn/go-sqlite3 v1.14.15

go version: go version go1.18.1 linux/amd64

  • Don’t use filename arguments to go commands. Make sure you have a correct go.mod file. – JimB Oct 22 '22 at 11:21
  • What is the reason for that, I just have one go file. – Ibrahim Sharaf Oct 22 '22 at 11:27
  • Because that is how the command works (no where in the docs does it show to use filename arguments). You need to build the package, which is part of a module, not a single file. Go through the “Getting Started” tutorial or “How to Write Go Code” – JimB Oct 22 '22 at 11:39
  • Never use go run with filename arguments, especially if you are a novice or having problems. – Volker Oct 22 '22 at 11:50
  • Use `-x` to see all commands being executed. Make sure you have a c compiler for the SQLite package and cgo – JimB Oct 22 '22 at 12:57
  • 1
    It also may help with confusion to not have a module named like one you are importing, and using a name that is a valid URI to prevent other future problems. – JimB Oct 22 '22 at 13:10

1 Answers1

0

If you use go with modules you could try to run go mod tidy