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
Here are the files in the question filled out. The import from db to models is illustrated. I set the module name to "my.example". Change that name to meet your needs. It's best to pick something including a "." to avoid conflict with a standard package. You can run this code on the Go PlayGround. How to Write Go Code explains all of this stuff in detail.
-- main.go --
-- go.mod --
-- cmd/config/config.go --
-- cmd/config/handlerr.go --
-- handlers/handlers.go --
-- pkg/models/models.go --
-- pkg/models/db/db.go --
-- pkg/router/router.go --
Regarding the layout of the files: This is in the land of opinions, so I will just ask you a question. Does the extra level of nesting in the
pkg
andcmd
gain you anything?