gpt4 book ai didi

go - goimports 未检测到本地包

转载 作者:数据小太阳 更新时间:2023-10-29 03:11:07 26 4
gpt4 key购买 nike

我的 golang 项目有一些包在不同的文件夹中,这个项目越来越大了。我一直在尝试创建单独的包,但没有被 goimports 检测到

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

我的目录是这样的,

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

在我的 math.go 文件夹中,我有 package utility

但是当我尝试执行 import "application/utility" 或当我尝试在我的 main.go 文件(在应用程序文件夹中)中包含 math.go 文件中的公共(public)函数时,我得到未定义的...

我做错了什么?

最佳答案

您没有向我们提供重现问题的具体步骤。我看不出有什么问题。 goimports -w main.go 有效。例如,

application/main.gogoimports -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.gogoimports -w main.go 之后:

package main

import (
"application/utility"
"fmt"
)

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

关于go - goimports 未检测到本地包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51584370/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com