对于golang项目,我在单独的文件夹中有一些软件包,这会变得有些大。我一直在尝试创建单独的程序包,但不会被goimports检测到
GOPATH="/home/malek/go:/home/malek/Desktop/Workspace"
GORACE=""
GOROOT="/usr/local/go"
我的目录就是这样,
Workspace -> src -> application -> utility -> math.go
在我的math.go文件夹中,我有打包实用程序
但是,当我尝试执行
import "application/utility"
或尝试将main.go文件(位于应用程序文件夹中)中math.go文件中的 public 函数包括在内时,我无法定义...我究竟做错了什么?
最佳答案
您尚未为我们提供重现问题的具体步骤。我没看到问题。 goimports -w main.go
有效。例如,application/main.go
之前的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 }
命令:
#
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
#
输出:
~$ #
~$ 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
之后的goimports -w main.go
:package main
import (
"application/utility"
"fmt"
)
func main() {
fmt.Println(utility.PiE())
}
关于go - goimports未检测到本地软件包,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51584370/