gpt4 book ai didi

go - 为什么Go在项目子目录中找不到导入包?

转载 作者:行者123 更新时间:2023-12-01 21:23:37 25 4
gpt4 key购买 nike

当我使用dev_appserver.py启动应用程序时,此导入工作正常

我正在尝试重构以使用Firestore and Go 1.13

app.go内容

package main
import (
"net/http"
"workout"
)

项目结构:
app.go  
|-- workout dir
|-- workout.go file that contains (package workout)

从工作目录的根目录运行:
$运行* .go
app.go:15:2: cannot find package "workout" in any of:
/usr/local/go/src/workout (from $GOROOT)
/Users/X/go/src/workout (from $GOPATH)
$ go run *.go workout/*.go
named files must all be in one directory; have ./ and workout/
X@MacBook-Pro Thu Oct 31 10:48:13 ~/Dropbox/go/src/workoutNew
$ go build   
app.go:15:2: cannot find package "workout" in any of:
/usr/local/go/src/workout (from $GOROOT)
/Users/X/go/src/workout (from $GOPATH)

最佳答案

import (
"workout"
)

这将尝试导入名为 workout的stdlib程序包。

为了导入 workout软件包,您应该命名主软件包(使用 go mod init),例如: github.com/me/myapp,然后将 workout软件包导入为 github.com/me/myapp/workout

也可以将 workout导入为相对目录(“./workout”),但这不是推荐的方式。

关于go - 为什么Go在项目子目录中找不到导入包?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58649962/

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