Hii, iam beginner on go lang with react and Fiber. Just started wih a tutorial picked up on youtube ( Golang API Authentication using JWT Tokens - YouTube ).
the author is using local import easily, but for my case i get error while try to run main.go
main.go:4:2: local import “./database” in non-local package
main.go:6:2: local import “./routes” in non-local package
down is the main.go ( i have created the structure as well)
please a help, iam blocked since 'days, trying to find a solution. ( i read that go doesnt support local import, but why is it working smoothly in that tutorial )
It imports fiber v1 and tries to use the middleware/cors package that only exists in fiber v2
So what I did to make the repo build without errors:
Step 1
In the root dir, I ran
go mod init mygoauth
where mygoauth is the name of the module that I simply made up. (This is not a library package and hence does not strictly need an importable module path, so we can be lazy and use an arbitrary name.)
You should now see a go.mod file with the module name inside.
Step 2
I now tried go mod tidy but got an error that the fiber lib does not contain middleware/cors. The error is correct: this package was added to fiber v2.
So the next step is to change ALL occurrences of
github.com/gofiber/fiber
in the .go files to
github.com/gofiber/fiber/v2
(same for github.com/gofiber/fiber/middleware/cors → put a v2/ after fiber/.)
And in the same step, also look for all local imports (starting with ./ or ../) and replace the single dot or double dots with mygoauth, e.g. mygoauth/database.
mygoauth is the name of the module, and inside a module we can use imports relative to the module root.
Step 3
run
go mod tidy
You should now have a go.mod file full of dependencies. Mine looks like so:
This is strange. I don’t see this at my end (I use a VSCode instance in a Gitpod.io workspace).
Compiling the code seems to work at your end, as I can see in the terminal. So the problem seems to be with VSCode.
Try restarting the language server (Command Palette > Go: Restart Language Server)
And maybe also check the VSCode Go Extension settings for custom settings like alternate tool paths that might enable wrong tools (or a wrong version of a tool)