11

I am creating a restful api in GO and every method essentially interacts with the database. The specific statement that I use to open a database connection is

db,err := sql.Open("postgres", "user=postgres password=password dbname=dbname sslmode=disable")
    if err != nil {
        log.Fatal(err)
        println(err)

    }

It is very simple but the issue is that once I want to change something inside that statement then I have to change it for all other methods that have that statement . I am trying to do a dependency injection or something of that nature so that I can have that statement or value in 1 place and just reference it. I am getting an import cycle not allowed error though like Import cycle not allowed . This is my project structure

enter image description here

What I have done is that in the Config.go I have written this

package Config

const Connect  = "user=postgres password=password dbname=dbname sslmode=disable"

Then in the Listings.go I put this

package Controllers

import (
    "net/http"
    "database/sql"

       "../Config"

)

func Listing_Expiration(w http.ResponseWriter, r *http.Request)  {


        db,err := sql.Open("postgres",Config.Connect)

        if err != nil {
            log.Fatal(err)
            println(err)

}

notice I have the import ../Config and the Config.Connect but when I compile that I get import cycle not allowed . I have been trying to solve this issue but haven't been able to.

Community
  • 1
  • 1
user1591668
  • 2,591
  • 5
  • 41
  • 84
  • 1
    Note that relative imports are discouraged: https://golang.org/cmd/go/#hdr-Relative_import_paths They are only meant to be used for simple experiments. – user1431317 Jul 16 '16 at 17:55
  • From your provided code I can't see the import loop. Also replace the relative import with an absolute import and let us know what happens. – TehSphinX Jul 18 '16 at 15:54

4 Answers4

13

Yes, Go doesn't allow to have cycled imports. In your example you have 2 packages Config and Controllers. When you build a code, Controllers package requires Config package, then Config requires Controllers and it's endless. You should refactor your code to make Config package separated from Controllers, and only used by it. Also, you can make some common package, imported to Controllers and Config.

Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
8

I got the same error. But in my case, I imported the package itself inside the file. so you can check if you did the same mistake.

VithuBati
  • 1,918
  • 1
  • 18
  • 26
1

Go does NOT allow import cycles. So you need to fix it. In your case, Controller and Config are importing each other, you are not allowed to do that.

I wrote a detailed blog about how you can deal with it. Refer https://jogendra.dev/import-cycles-in-golang-and-how-to-deal-with-them

  1. Use interfaces.
  2. Making use of go:linkname.
Jogendra Kumar
  • 894
  • 11
  • 16
-1

Though, I can't see any import cycle from your code. Go don't support the import cycle. Based on your question, you must import the controller from the Config Package. You need to remove one of them and refactor your code. Fore more: https://jogendra.dev/import-cycles-in-golang-and-how-to-deal-with-them