Search code examples
go

Broken Import - Cannot find package in a sub directory


Directory structure:

src
    go.mod
    lib/migrate
        migrate.go
        /driver/source
            local.go

I'm trying to import local in migrate.go like this:

package migrate

import (
    local "app/lib/migrate/driver/source/local"
)

type Migrate struct {
}

And I'm receiving this error:

could not import app/lib/migrate/driver/source/local (no required module provides package "app/lib/migrate/driver/source/local")

I checked the below items:

  1. go.mod declares module app
  2. local.go declares package local
  3. go env -w GO111MODULE=on

Solution

  • to get what you want, you have two options:

    1. Replace import local "app/lib/migrate/driver/source/local" with local "app/lib/migrate/driver/source"
    2. Create a folder named local(to contain local package under source) and place local.go inside it and replace the import to "app/lib/migrate/driver/source/local", (I don't recommend this one!)

    I recommend to always use folder name as package name, in your case import "app/lib/migrate/driver/source" and use source to get to local.go exported things.