Relative import link doesn't work between packages

I have very simple project on GO:

  • Main.go
  • Readers(folder)
    • a.go
    • b.go

Main.go import package Readers and call function ShowArgumets

import ( "fmt"
 "os"
 "./Readers"
)      
func main() {
  fmt.Println("Start dev-ops tool.")
  Readers.ShowArguments(os.Args)
}

File A.go contain function ShowArguments:

package Readers

import ( "fmt")

func ShowArguments(incomming []string)*GeneralConfig{
  fmt.Println("Read arguments:")
  fmt.Println("Total arguments: %s", len(incomming))

  loadingWorkflows:=GeneralConfig{}

   for i := 1; i < len(incomming); i++ {
       fmt.Println(incomming[i])
       fmt.Println("============")
   }
   return &loadingWorkflows
} 

In file B.go, I’ve added structure, which in the future will return to main some structure of information about incoming arguments :

package Readers

type GeneralConfig struct {  
  RootFolder   string
  OutputFolder    string
}

This scenario perfectly works. But I’ve decided move file B.go to other packages:Helpers.

Now file A.go looks like:

package Readers

import ( "fmt"
        _"../Helpers"
)

func ShowArguments(incomming []string)*Helpers.GeneralConfig{
  fmt.Println("Read arguments:")
  fmt.Println("Total arguments: %s", len(incomming))

  loadingWorkflows:=Helpers.GeneralConfig{}

  for i := 1; i < len(incomming); i++ {
      fmt.Println(incomming[i])
      fmt.Println("============")
  }
  return &loadingWorkflows
}

And file B.go:

package Helpers
       type GeneralConfig struct {  
       RootFolder   string
       OutputFolder    string
}

Structure of files:

  • Main.go
  • Readers
    • A.go
  • Helpers
    • B.go

When I build this project I’ve received errors:

Readers\A.go:7:40: undefined: Helpers

Readers\A.go:11:20: undefined: Helpers

It could find Helpers directory, but can’t find Helpers package.
Could you please advice how to resolve issue?

P.S. Please don’t give me advice like: read GoLang for begginer. I’m already read this. The problem is that I need use relative path in code, because I can’t use $GOPATH and other general path. My goal: place code in any directory, execute build command and start use compiled exe.

This means, import and run init(), but do not make the package available, you should remove the underscore.

1 Like

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