英文:
Unable to use functions within imported package
问题
我的当前目录设置如下:
-- /webserver
|
/ftp
|-> ftp.go
|-> go.mod
|->server.go
|->go.mod
Server.go 页面标题
package main
import (
[..]
"../webserver/ftp" // 问题包
)
func main() {
[..]
ftp.test()
}
ftp.go 页面
package ftp
import "fmt"
func test() {
fmt.Println("YES!")
}
这是我在控制台“问题”选项卡中无法理解的部分。ftp.go 文件被保存。在我用“../webserver/ftp/”保存 server.go 文件之前,控制台显示如下:
当我保存 server.go 文件时,它会删除“./webserver/ftp”,我甚至将其添加到 go src 中,使其变为“ftp”而不是“../webserver/ftp”。但结果是一样的。
不确定我做错了什么。
基本上,我想要实现的是:
在一个 functions 文件夹中,将函数放在该文件夹中(functions.go),以使 server.go 页面尽可能清晰。
英文:
My current Directory is setup as follows:
-- /webserver
|
/ftp
|-> ftp.go
|-> go.mod
|->server.go
|->go.mod
Server.go page heading
package main
import (
[..]
"../webserver/ftp" // issue package
)
func main() {
[..]
ftp.test()
}
ftp.go page
package ftp
import "fmt"
func test() {
fmt.Println("YES!")
}
Here's the part I cant seem to understand in the console "PROMBLEMS" tab. The ftp.go file gets saved. Before I save the server.go file with "../webserver/ftp/" the console displays this:
When I save server.go it removes "./webserver/ftp", I went as far as adding it into the go src to make it "ftp" instead of "../webserver/ftp". I get the same result.
Not sure what I am doing wrong.
Basically what i want to accomplish is:
have a functions folder and have the functions in that folder (functions.go) to keep the server.go page as clean as possible.
答案1
得分: 5
你这里有几个问题。
-
ftp/go.mod
文件不应该存在。虽然在一个仓库中可以有多个 Go 模块,但这是非常高级的用法,很少有用。除非你确切地知道你需要这个功能,并且已经非常熟悉创建和发布 Go 模块,否则不要这样做。 -
Go 不支持相对导入。
import "../webserver/ftp"
是无效的导入路径。使用完全限定的导入路径,例如import "github.com/username/projectname/webserver/ftp"
。这里要使用的路径取决于你在顶级
go.mod
文件的module
行中声明的模块名。 -
你不能导入未导出的值。
你在
ftp.go
中定义了func test() string
。由于以小写字母开头,这是未导出的。在 Go 中,只有以大写字母开头的函数、变量或其他符号才会被导出。尝试使用func Test() string
。
*有人会说:“但是我使用 import ".../foo"
,它可以工作!”是的,在非常特定的情况下它可以工作。但它的行为是不可靠的,所以除非在非常特定的测试类型的情况下,否则不要依赖它。
英文:
You have several problems here.
-
ftp/go.mod
should not exist. While it is possible to have multiple Go modules in a single repository, this is very advanced usage, and is rarely useful. Unless you absoultely know that you need this capability, and are already exremely comfortable with creating and publishing Go modules, don't do this. -
Go does not support relative imports.
import "../webserver/ftp"
is an invalid import path*. Use the fully qualified import path, i.e.import "github.com/username/projectname/webserver/ftp"
The path to use here depends on the module name you've declared on the
module
line of your your top-levelgo.mod
file. -
You cannot import unexported values.
You've defined
func test() string
inftp.go
. This is unexported, by virtue of beginning with a lowercase letter. Only functions, variables, or other symbols which begin with a capital letter are exported in Go. Tryfunc Test() string
instead.
*Some will say "But I used import ".../foo"
and it worked!". Yes, it can work under very specific circumstances. But its behavior is not reliable, so you should never rely on this, except in very specific testing types of situations.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论