"import cycle not allowed"

Hi, I know this is probably a common issue, but from ~an hour of googling I haven’t been able to solve it…

I have a go.mod generated via
go mod init github.com/go-sql-driver/mysql

module github.com/go-sql-driver/mysql

go 1.16

And a main.go file:

package main

import (
    "database/sql"
    _ "github.com/go-sql-driver/mysql"
)

func main() {

}

And when I run go run main.go
I get

package command-line-arguments
	imports github.com/go-sql-driver/mysql
	imports github.com/go-sql-driver/mysql: import cycle not allowed

Hi, @jvannier, welcome to the forum.

I suspect it’s because you named the module “GitHub - go-sql-driver/mysql: Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package” here:

But the module name you specify in that command is supposed to be the module name of your own code. When you import “GitHub - go-sql-driver/mysql: Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package” here:

You’ve “confused” the Go compiler by telling it that your own package is called “GitHub - go-sql-driver/mysql: Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package”, but then imported a module with that same package identifier.

That was the issue! I was misunderstanding what the mod init did. Thank you for your help!!

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.