I want to use this package: github.com/go-sql-driver/mysql
I tried with:
go get -u github.com/go-sql-driver/mysql
It went right, but when I'm importing the package and running the code its gives me an error:
main.go:12:2: no required module provides package github.com/go-sql-driver/mysql: go.mod file not found in current directory or any parent directory; see 'go help modules'
CodePudding user response:
In project root directory run
go mod init example.com/your-project-name
It will create go.mod
file
module example.com/your-project-name
go 1.15
Then run
go get -u github.com/go-sql-driver/mysql
CodePudding user response:
EDIT: As rustyx said in the comment, this is a deprecated approach and you might want to consider newer ones
You can do without the go.mod
if your GOROOT
and GOPATH
environment variables are setted correctly
To do so, you can refer to
What should be the values of GOPATH and GOROOT?
Also, you need to ensure that your IDE/text editor also recognize those two variables.
Note: In the case of an IDE (such as Goland), you might need to set it up manually. In the case of a text editor (ex: vscode), you can simply reload your project and it should do the trick.
Once this is done, you can validate that the content of your $GOPATH/github.com/go-sql-driver/mysql/ is present. If not, you can rerun your go get -u github.com/go-sql-driver/mysql
command, reload your editor, and you should be alright