"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.com/go-sql-driver/mysql” 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.com/go-sql-driver/mysql” here:

You’ve “confused” the Go compiler by telling it that your own package is called “github.com/go-sql-driver/mysql”, 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.