从 GitHub 存储库导入 go-sql-driver/sql 时出现错误。

huangapple go评论83阅读模式
英文:

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")

screenshot of what's happening

答案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 testgo 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 to main.
  • 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 running go mod init test and go 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)

huangapple
  • 本文由 发表于 2021年11月20日 01:38:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/70038792.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定