0

I have a few packages in separate folders for my golang project that is getting a bit big. I've been trying to create separate packages but don't get detected by goimports

GOPATH="/home/malek/go:/home/malek/Desktop/Workspace"
GORACE=""
GOROOT="/usr/local/go"

My directory is as such,

Workspace -> src -> application -> utility -> math.go

and in my math.go folder, I have package utility

But when I try to do import "application/utility" or when i try to include a public function from the math.go file in my main.go file (in application folder), I get undefined...

What am I doing wrong?

Whiteclaws
  • 902
  • 10
  • 31
  • You have 2 parts in your GOPATH, what is “Workspace”? Also don’t set GOROOT. – JimB Jul 29 '18 at 20:51
  • Also, if “application/utility” is a proper package, you should be able to run `go build application/utility` without error, or it may provide some information about the problem with your configuration. – JimB Jul 29 '18 at 21:06
  • @JimB It builds without a message or error – Whiteclaws Jul 29 '18 at 21:18
  • @JimB: From the context, the `GOROOT` value in the question looks like part of the output from `go env`. If GOROOT is not set (I don't set it), the Go tool chain sets it to the implicit GOROOT for the `go` command. I have `$ echo $GOROOT\n\n` and `$ go env GOROOT\n/home/peter/go\n`. – peterSO Jul 29 '18 at 22:23
  • @Whiteclaws: then you need to create a [mcve], since it should work if everything is configured in a standard manner. – JimB Jul 30 '18 at 00:22
  • 1
    Possible duplicate of [Go build: "Cannot find package" (even though GOPATH is set)](https://stackoverflow.com/questions/13214029/go-build-cannot-find-package-even-though-gopath-is-set) – Bert Verhees Jul 30 '18 at 07:22

1 Answers1

1

You haven't provided us with specific steps to reproduce your problem. I don't see a problem. goimports -w main.go works. For example,

application/main.go before goimports -w main.go:

package main

import (
    "fmt"
)

func main() {
    fmt.Println(utility.PiE())
}

application/utility/math.go:

package utility

import "math"

func PiE() float64 { return math.Pi * math.E }

Commands:

#
cd $HOME
cd $HOME/Desktop/Workspace
rm -f src/application/application
cp src/application/main.bak src/application/main.go
tree
cd $HOME/Desktop/Workspace/src/
cat application/utility/math.go
cd application
export GOPATH=$HOME/gopath:$HOME/Desktop/Workspace
go env GOPATH
cat main.go
go build -a
goimports -w main.go
cat main.go
go build -a && ./application
#

Output:

~$ #
~$ cd $HOME
~$ cd $HOME/Desktop/Workspace
~/Desktop/Workspace$ rm -f src/application/application
~/Desktop/Workspace$ cp src/application/main.bak src/application/main.go
~/Desktop/Workspace$ tree
.
└── src
    └── application
        ├── main.bak
        ├── main.go
        └── utility
            └── math.go

3 directories, 3 files
~/Desktop/Workspace$ cd $HOME/Desktop/Workspace/src/
~/Desktop/Workspace/src$ cat application/utility/math.go
package utility

import "math"

func PiE() float64 { return math.Pi * math.E }
~/Desktop/Workspace/src$ cd application
~/Desktop/Workspace/src/application$ export GOPATH=$HOME/gopath:$HOME/Desktop/Workspace
~/Desktop/Workspace/src/application$ go env GOPATH
/home/peter/gopath:/home/peter/Desktop/Workspace
~/Desktop/Workspace/src/application$ cat main.go
package main

import (
    "fmt"
)

func main() {
    fmt.Println(utility.PiE())
}
~/Desktop/Workspace/src/application$ go build -a
# application
./main.go:8:14: undefined: utility
~/Desktop/Workspace/src/application$ goimports -w main.go
~/Desktop/Workspace/src/application$ cat main.go
package main

import (
    "application/utility"
    "fmt"
)

func main() {
    fmt.Println(utility.PiE())
}
~/Desktop/Workspace/src/application$ go build -a && ./application
8.539734222673568
~/Desktop/Workspace/src/application$ #

application/main.go after goimports -w main.go:

package main

import (
    "application/utility"
    "fmt"
)

func main() {
    fmt.Println(utility.PiE())
}
peterSO
  • 158,998
  • 31
  • 281
  • 276
  • I reproduced your example with a simple application: https://pastebin.com/NagedTLK – Whiteclaws Jul 30 '18 at 17:07
  • Oh, and also, calling go install application/utility does give me a package – Whiteclaws Jul 30 '18 at 17:10
  • Your pastebin output stopped before the `goimports -w main.go` and `cat main.go` steps. – peterSO Jul 30 '18 at 17:44
  • goimports main.go is the equivalent of goimports -w main.go and cat main.go without changing main.go, in other words, goimports -w main.go would also print the same thing... but yeah, goimports -w main.go doesn't import "application/utility" – Whiteclaws Jul 30 '18 at 18:59