Import cycle not allowed

I’ve my structure as bellow:

image

in the main.go I import both modules lib and models as:

package main

import (
	"aging/lib"
	"aging/models"
)

Each file at folder models start by:

package models

And none of the files in the models have any import statement of lib

Each file at folder lib start by:

package lib

And none of the files in the lib have any import statement of models

Things are running smoothly.

I wanted to use a model in one of the files at lib so at that file (the one in the lib) I wrote:

package lib

import (
	"aging/models"
	"encoding/csv"
	"log"
	"strconv"
)

// Write the data to the selected csv file
func WriteOutputs(data models.Inventories, file *csv.Writer) { }

Then things collapsed and I got the error below:

import cycle not allowed
package aging
        imports aging/lib
        imports aging/models
        imports aging/lib

i do not understand how this cycle happen, as I see my app it is as below:

main ----> models
       \            
        \--> lib ------> models

Where is my misunderstanding?

One of your files in the models package refers to the lib package please double-check.

Show more code if you don’t find it.

BTW you shouldn’t create packages by types but by functionality. It helps with such issues.

3 Likes

Though there are only 3 files in models and I checked them couple of time before posting, looks I was tired so I did not see it, you are correct, there was one and I fixed it, thanks

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