Error at run : local import "./database" in non-local package

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)

package main

import (

func main() {

  • database.Connect()*

  • app := fiber.New()*

  • routes.Setup(app)*

  • app.Listen(":3000")*
    }

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 )

Hi @sakis,

Welcome to the forum.

I see three issues with the code from the YT video:

  • It uses local imports, as you pointed out
  • It does not use Go Modules
  • 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:

module mygoauth

go 1.17

require (
	github.com/dgrijalva/jwt-go v3.2.0+incompatible
	github.com/gofiber/fiber/v2 v2.24.0
	golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
	gorm.io/driver/mysql v1.2.3
	gorm.io/gorm v1.22.5
)

require (
	github.com/andybalholm/brotli v1.0.2 // indirect
	github.com/go-sql-driver/mysql v1.6.0 // indirect
	github.com/jinzhu/inflection v1.0.0 // indirect
	github.com/jinzhu/now v1.1.4 // indirect
	github.com/klauspost/compress v1.13.4 // indirect
	github.com/valyala/bytebufferpool v1.0.0 // indirect
	github.com/valyala/fasthttp v1.31.0 // indirect
	github.com/valyala/tcplisten v1.0.0 // indirect
	golang.org/x/sys v0.0.0-20210514084401-e8d321eab015 // indirect
)

If you now run go build you should get no errors and a binary named “mygoauth”.

Let me know if these steps help

Hi Sir, thank you for your help, really i was pissed off since many days, thank.

Following your steps, all work fine, go build was made whithout error.

Now, i have to manage with error in each go files, even they work fine, it still prompt error ( see down )

down is the error prompted. just a little bit hash to program with red color everywhere in workspace

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)

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