对于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文件中的公共函数包括在内时,我会得到未定义...
我在做什么错了?
答案 0 :(得分:1)
您尚未为我们提供具体步骤来重现您的问题。我没问题。 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())
}