无法在导入的包中使用函数。

huangapple go评论70阅读模式
英文:

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

你这里有几个问题。

  1. ftp/go.mod 文件不应该存在。虽然在一个仓库中可以有多个 Go 模块,但这是非常高级的用法,很少有用。除非你确切地知道你需要这个功能,并且已经非常熟悉创建和发布 Go 模块,否则不要这样做。

  2. Go 不支持相对导入。import "../webserver/ftp" 是无效的导入路径。使用完全限定的导入路径,例如 import "github.com/username/projectname/webserver/ftp"

    这里要使用的路径取决于你在顶级 go.mod 文件的 module 行中声明的模块名。

  3. 你不能导入未导出的值。

    你在 ftp.go 中定义了 func test() string。由于以小写字母开头,这是未导出的。在 Go 中,只有以大写字母开头的函数、变量或其他符号才会被导出。尝试使用 func Test() string

*有人会说:“但是我使用 import ".../foo",它可以工作!”是的,在非常特定的情况下它可以工作。但它的行为是不可靠的,所以除非在非常特定的测试类型的情况下,否则不要依赖它。

英文:

You have several problems here.

  1. 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.

  2. 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-level go.mod file.

  3. You cannot import unexported values.

    You've defined func test() string in ftp.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. Try func 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.

huangapple
  • 本文由 发表于 2021年8月9日 18:00:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/68710184.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定