简体   繁体   中英

Local packages are not detected by goimports

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?

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())
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM