"undefined" import but I see the struct when I hover over the variable

Hi, I’ve been stuck on can’t this and can’t compile, checked my variable names and imports start with github…My other external package imports are fine. Could there be an issue with the import because of the structure with there being a subdirectory? I did create a go.mod file that utilizes the parent directory.

In my adapter file I’m getting an error: undefined: "github.com/org/app/internal/config".ServerConfig

However, in VS code when I hover over that variable and I see the struct I defined just fine.

adapter code github.com/org/app/subdirectory/pkg/adapters/adapter.go:


import (
    "github.com/org/app/internal/config"
)

type ServerAdapter struct {
    config          config.serverConfig
}

func (h *ServerAdapter) Setup(cfg *config.serverConfig) {
    //do something
}

config structs code github.com/org/app/internal/config/app.go:


type AppConfig struct {
    Env     string
    Server  ServerConfig

}

type ServerConfig struct {
    Domain   string
    Service  string
    HostPort string
}

main file code github.com/org/app/subdirectory/cmd/main.go:


import (
    adapter "github.com/org/app/subdirectory/pkg/adapters"
    "github.com/org/app/internal/config"
)


func main() {
    cfg := config.New()
    cfg.Print()
}

Welcome to Golang Bridge! :rocket: :fireworks:


internal is a special directory dedicated to only the parent directory import. That means only github.com/org/app or github.com/org/app/internal/sibilings can import github.com/org/app/internal/config and no others.

Since you’re importing as a third-party perspective, it will fail. If you need the config directory, you must promote it outside of internal directory. See https://github.com/golang-standards/project-layout for understanding the project directory structure.

1 Like

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