英文:
Error on importing go-sql-driver/sql from github repository
问题
正如标题所说,我在导入go-mysql-driver包时遇到了错误。我已经在我的机器上安装了go-my-sql驱动程序,但错误仍然存在。我在本地使用XAMPP进行托管,以下是程序的代码块。
package model
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type Table interface {
Name() string
Field() ([]string, []interface{})
}
func Connect(username string, password string, host string, database string) (*sql.DB, error) {
conn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", username, password, host, database)
db, err := sql.Open("mysql", conn)
return db, err
}
func CreateDB(db *sql.DB, name string) error {
query := fmt.Sprintf("CREATE DATABASE %v", name)
_, err := db.Exec(query)
return err
}
func CreateTable(db *sql.DB, query string) error {
_, err := db.Exec(query)
return err
}
func DropDB(db *sql.DB, name string) error {
query := fmt.Sprintf("DROP DATABASE %v", name)
_, err := db.Exec(query)
return err
}
无法导入github.com/go-sql-driver/mysql(没有所需的模块提供包“github.com/go-sql-driver/mysql”)
英文:
As the title says, I have an error when importing go-mysql-driver package. I have installed the go-my-sql driver in my machine but the error still persists. I use XAMPP for local hosting and here’s the block of program.
package model
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
)
type Table interface {
Name() string
Field() ([]string, []interface{})
}
func Connect(username string, password string, host string, database string) (*sql.DB, error) {
conn := fmt.Sprintf("%s:%s@tcp(%s:3306)/%s", username, password, host, database)
db, err := sql.Open("mysql", conn)
return db, err
}
func CreateDB(db *sql.DB, name string) error {
query := fmt.Sprintf("CREATE DATABASE %v", name)
_, err := db.Exec(query)
return err
}
func CreateTable(db *sql.DB, query string) error {
_, err := db.Exec(query)
return err
}
func DropDB(db *sql.DB, name string) error {
query := fmt.Sprintf("DROP DATABASE %v", name)
_, err := db.Exec(query)
return err
}
could not import github.com/go-sql-driver/mysql (no required modules provides package "github.com/go-sql-driver/mysql")
答案1
得分: 1
你的IDE没有显示完整的情况。通过在命令行上运行go run main.go
(或者你的主文件)你可以看到与你在IDE上看到的相同的错误,还有一些额外的信息:
$ go run main.go
main.go:7:5: no required module provides package github.com/go-sql-driver/mysql; to add it:
go get github.com/go-sql-driver/mysql
通过执行建议的命令go get github.com/go-sql-driver/mysql
,你将把该依赖项添加到你的go.mod
文件中,并将包的内容下载到你的计算机上。
你的下一次执行将会成功:
$ go run main.go
Hello world
我对你的代码进行了一些小的修改,使其能够工作,我将它们添加在这里以供完整性:
- 我使用了相同的源代码,但将
package
名称更改为main
。 - 我在文件底部添加了一个
main
函数:
func main() {
fmt.Println("Hello world")
_, err := Connect("username", "password", "localhost", "db")
if err != nil {
panic(err)
}
}
- 我将文件保存为
main.go
。 - 我通过运行
go mod init test
和go mod tidy
来初始化go.mod
文件,然后按照答案开头的步骤进行操作。
英文:
Your IDE is not showing you the whole picture. By running go run main.go
(or whatever main file you have) on the command line, you can see the same error as you're seeing on your IDE with some extra:
$ go run main.go
main.go:7:5: no required module provides package github.com/go-sql-driver/mysql; to add it:
go get github.com/go-sql-driver/mysql
By issuing the suggested command go get github.com/go-sql-driver/mysql
, you'll get the dependency added to your go.mod
file and the contents of the package will be downloaded to your machine.
Your next execution will work:
$ go run main.go
Hello world
I've made some small modifications to your code to work, I'll add them here for completeness:
- I've used the same source, but changed the
package
name tomain
. - I've added a main function to the bottom of the file:
func main() {
fmt.Println("Hello world")
_, err := Connect("username", "password", "localhost", "db")
if err != nil {
panic(err)
}
}
- I've saved to a file named
main.go
- I've initialized the
go.mod
file by runninggo mod init test
andgo mod tidy
, then I took the steps described on the beginning of the answer.
答案2
得分: 1
看起来你正在阅读一个较旧版本的Go教程。
Go 1.17要求依赖必须明确写在go.mod
文件中。
也许你可以先尝试使用Go模块(https://go.dev/blog/using-go-modules)。
英文:
It seems that you read the tutorial for an older go version.
Go 1.17 requires dependencies must be explicitly in go.mod
.
Maybe you could try go module first (https://go.dev/blog/using-go-modules)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论