-3

Assume my project's file structure is this:

.
├── main.go
└── ui
    ├── controls
    │   ├── control.go
    │   ├── constants.go
    │   ├── container.go
    │   ├── label.go
    └── views
        └── view.go

All files in ui/controls are in a package called controls and view.go is in the views package. I want code in the views package to be able to reference an interface defined in control.go.

I'm using Go 1.14. Is what I want possible at all?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Peter W.
  • 2,323
  • 4
  • 22
  • 42

1 Answers1

2

Absolutely. You need to provide a full path on import, as usual.

E.g. if you module is github.com/peterw/myproject (*), then to import the controls package from a file in the views package, you'd do:

import "github.com/peterw/myproject/ui/controls"

(*) This means that in your go.mod file the module is defined with:

module github.com/peterw/myproject
Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
  • 1
    Define "full path", please. I need to be able to compile this project on various platforms and I can't assume the project folder will always have the same name or be in the same location in the file system. – Peter W. Mar 14 '20 at 23:18
  • @PeterW.: if you're not familiar with using Go modules, consider reading something like https://eli.thegreenplace.net/2019/simple-go-project-layout-with-modules/ and then the official docs – Eli Bendersky Mar 14 '20 at 23:20
  • Go depends excessively on source control systems ... – Charlie Reitzel Apr 20 '22 at 22:22
  • 1
    You literally cannot build your code (let alone run unit tests!) without checking it in and pushing to the repository ... what am I missing ? – Charlie Reitzel Apr 20 '22 at 22:35