I have following folder structure here
server
-main.go
-cmd
-config
-config.go
-handlerr.go
-handlers
-handlers.go
-pkg
-models
-models.go
-db
-db.go
-router
-router.go
and when I am trying to import the "models package" into db package it says "invalid import path:...",this structure i am following with book ,so what am i doing wrong ?How do i import the models into db functions or should i replace them (db and models),any suggest? enter image description here
UPDATE: As @maxm reminded me, use
GOPATH, here is a great example, that he mentioned.So, if my web search and a little example program are correct, this is pretty much what is happening here.
Golang sees local packages as folders with .go files, on practice
/modules/module.goifmodules.gohaspackage moduleson the 1st line.So, when we are including a local package, we are actually including a folder. I created a little example:
main.go
modules/modules.go
modules/printer/printer.go
And this actually works! So, you can import a parent module, you just need to include the folder.
Of course, we cannot have import cycle, Golang won't allow it and if you are having this kind of error, it's better to change the structure of your program.
I'm not talking about paths and stuff, it's just a practical solution. So, smart people, please correct me!