Search code examples
postgresqlgoimportpq

How can I find the problem with Go failed import?


While working on a Go web-app project (for learning), I have encountered the following issue:

At the beginning, everything was alright. I imported packages from the standard library, used them in the code and everything worked. up to the moment when I have tried to import the pq driver for PostgreSQL.

The actions that I did in detail: The folder with the project files inside: notes.

project is in the directory: C:\Users\david\go\src\github.com\davidkuch\notes

When starting, i run the command: go mod init.

I imported the standard package "database/sql".

to download the package I used: go get "github.com/lib/pq"

after that- go mod tidy

but the compiler says:could not import {package-name} no required module provides package {package-name}

I tried to read through the docs of the related topics, but couldn't find where I did a mistake. the same happens for another package i have tried to install from GitHub.

where should I be looking to find the problem? as the compiler says that he "cannot find", I made a lot of effort checking naming and paths. but I see the package exactly in the path I try to import from. to be more precise:

After some hours of trying to fix that by myself, I ask You for some help or explanation of what is happening.


Solution

  • project is in the directory: C:\Users\david\go\src\github.com\davidkuch\notes

    You don't need to do that. Just make a folder like: C:\Users\david\notes.

    Then make C:\Users\david\notes\main.go:

    package main
    
    import (
       "database/sql"
       _ "github.com/lib/pq"
    )
    
    func main() {
       println(sql.ErrNoRows) 
    }
    

    Then build:

    go mod init something
    go mod tidy
    go build