GO(Golang)无法看到同一目录中的其他.go文件。

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

GO (Golang) does not see other .go files inside the same dir

问题

问题: 从其他包导出的函数在main.go内部未定义,无法访问。

解决方案:
main.go放入一个单独的文件夹中(命名为app或main,无所谓)。只有go.mod保留在根目录下,其中包括文件夹"app"和"package1"、"package2"等。在此之后,VSCode会在保存时自动添加导入,我甚至不需要做任何操作。如果我将main.go放在根目录下,它就不起作用(大多数情况下),因为它无法"看到"其他模块的函数(未定义)。

我在这里找到了解决方案(参见davidbost的帖子)。这个页面上的Andrey Dyatlov的解决方案一度起作用,然后停止起作用。

我花了大约10个小时的尝试、错误和搜索。
希望上述内容对其他人有所帮助。谢谢大家!

_______________________________________________________________________

原始问题描述:

Windows 10,Go 1.17,带有Go扩展的VS Code。

你好,我是Go的新手,由于以下问题,我无法按照任何教程进行操作。当我在与main.go相同的目录(或同一目录的文件夹内)中创建另一个.go文件时,我会收到错误消息,内容为**.\main.go:7:2: undefined: SayHi**。


main.go文件:

package main

import "fmt"

func main() {
    fmt.Println("1st")
    SayHi()
}

在/something文件夹内的另一个.go文件:

package something

import "fmt"

func SayHi() {
    fmt.Println("Hi!")
}

运行go run main.go会导致未定义的SayHi错误。

我在谷歌上搜索了这个问题,但没有找到解决办法。

$ go build // 出现相同的错误

$ go install // 出现相同的错误

如果不使用其他文件中的函数,$ go run main.go可以正常运行。

我还尝试了使用go init并添加我的GitHub目录,但没有成功(按照教程进行操作)。我还尝试了官方的入门指南,使用go run init,但导出的C大写函数仍然未定义。更糟糕的是,SayHi函数的自动完成可以正常工作,但由于未定义,它无法编译。

我已将PATH设置为C:\Users\xxx\go,并将我的文件放在那里,我还尝试了C:\Go,但没有成功,它仍然未定义。我快要放弃Go了...

英文:

PROBLEM: functions exported from other packages are undefined, invisible from inside of main.go

SOLUTION:
Put main.go into a separate folder (name it app or main, doesn't matter). Only go.mod remains in root, with the folders "app" and "package1" "package2" etc. After this VSCode automatically added imports on save, I didn't even need to do anything. If I have main.go in root then it doesn't work (most of the time) as it doesn't "see" functions from other modules (undefined).

I found the solution HERE,(see post by davidbost). The solution on this page by Andrey Dyatlov worked too for a while and then stopped working.

It took me probably 10 hours of try and error and searching.
Hopefully the above will help others. Thank you, everyone!

_______________________________________________________________________

Original Problem Description:

Windows 10, Go 1.17, VS Code with Go extension.

Hello, I am new to Go and I was not able to follow a single tutorial due to the following issue. When I create another .go file in the same directory (or inside a folder of the same directory) as the main.go, I receive error saying .\main.go:7:2: undefined: SayHi


main.go file:

package main

import "fmt"

func main() {
	fmt.Println("1st")
	SayHi()
}

another .go file inside /something folder:

package something

import "fmt"

func SayHi() {
	fmt.Println("Hi!")
}

Running go run main.go results in the undefined SayHi error

I googled the issue with no luck.

$ go build // gives out the same error

$ go install // gives out the same error

without using functions from other files $ go run main.go runs just fine.

I also tried go init with adding my github directory with no luck (by following a tutorial). I also tried the official starting guide with go run init, and the exported Capitalized function is still undefined. What's worse is that autocomplete for the SayHi function works, yet it won't compile because undefined.

I have set PATH to C:\Users\xxx\go and put my files there, I also tried using C:\Go with no luck, it's still undefined. I'm about to give up on Go...

答案1

得分: 5

很难在不了解项目当前状态的情况下修复您的项目。请尝试从头开始进行以下步骤:

  1. $GOPATH之外的任何位置创建一个项目目录:

    mkdir myproject
    cd myproject
    
  2. 运行以下命令创建一个描述项目(模块)及其依赖关系的go.mod文件;我们将模块称为github.com/me/myproject

    go mod init github.com/me/myproject
    
  3. 创建第一个文件,我们称之为main.go

    package main
    
    import "fmt"
    import "github.com/me/myproject/something"
    
    func main() {
        fmt.Println("1st")
        something.SayHi()
    }
    
  4. 为名为github.com/me/myproject/something的包创建一个目录:

    mkdir something
    
  5. 创建路径为something/something.go的文件:

    package something
    
    import "fmt"
    
    func SayHi() {
        fmt.Println("Hi!")
    }
    
  6. myproject目录中运行go build

  7. 启动您的第一个Go程序:

    ./myproject
    1st
    Hi!
    
英文:

It' would be hard to fix your project without knowing its current state. Please, try to start from scratch:

  1. Create a directory for your project anywhere outside $GOPATH:

    mkdir myproject
    cd myproject
    
  2. Run the following command to create a go.mode file that describes your project (module) and its dependencies; let's call the module github.com/me/myproject:

    go mod init github.com/me/myproject
    
  3. Create the first file; let's call it main.go:

    package main
    
    import "fmt"
    import "github.com/me/myproject/something"
    
    func main() {
        fmt.Println("1st")
        something.SayHi()
    }
    
  4. Create a directory for the package called github.com/me/myproject/something:

    mkdir something
    
  5. Create a file with path something/something.go:

    package something
    
    import "fmt"
    
    func SayHi() {
        fmt.Println("Hi!")
    }
    
  6. From the myproject directory, run go build.

  7. Launch your first Go program:

    ./myproject
    1st
    Hi!
    

huangapple
  • 本文由 发表于 2021年11月19日 01:50:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/70024480.html
匿名

发表评论

匿名网友

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

确定