John Doah
John Doah

Reputation: 1999

Can't import packages

I'm trying to import a subdirectory I have in my project to my main file, but for some reason I get this error:

could not import ./service (no package for import ./service)

This is how I import:

import (
"os"

"service"

)

I also tried "service/app"

"./service/app"

"./service" nothing works, always the same error.

I'm trying to use the app.go file This is the file structure:

Project Directory
        -- main.go
        -- service (directory)
           -- app.go (file)

I tried to restart VS Code, tried to use go install/update tools, nothing works. Also, this is my main func:

func main() {
  a := &service.App()
  a.Initialize(
    os.Getenv("APP_DB_USERNAME"),
    os.Getenv("APP_DB_PASSWORD"),
    os.Getenv("APP_DB_NAME"),
  )

  a.Run(":8010")
}

&service.App() does not show a problem, but when I remove the "service" import, it does say

undeclared name: service

So I don't understand what the problem is. This error sometime show on the "os" import too, I don't know why.

Upvotes: 0

Views: 559

Answers (1)

Grigoriy Mikhalkin
Grigoriy Mikhalkin

Reputation: 5573

Golang doesn't support relative imports.

In modules, there finally is a name for the subdirectory. If the parent directory says "module m" then the subdirectory is imported as "m/subdir", no longer "./subdir".

So, in your case you should import service package as your_module_name/service.

Upvotes: 1

Related Questions