从与主文件夹相同的父文件夹中的结构体不可见。

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

Struct from the same parent folder as main is not visible

问题

我在Gogland中有一个小的Go演示项目,结构如下:

awsomeProject
    ->src
        ->awsomeProject
            ->configuration.go
            ->main.go

配置文件只是为了演示而具有简单的结构:
<br>configuration.go:

package main

type Config struct {
    Data int
}

主文件只使用Config结构体:
<br>main.go:

package main

import "fmt"

func main(){
    var cfg Config
    cfg.Data = 1
    fmt.Println("lalala")
}

我遇到的错误是:

> /usr/local/go/bin/go run /Users/lapetre/Work/awsomeProject/src/awsomeProject/main.go
command-line-arguments
src/awsomeProject/main.go:6: undefined: Config
Process finished with exit code 2

有任何想法为什么在main函数中无法看到Config?
<br>谢谢

英文:

I have a small go demo project in Gogland with the structure:

awsomeProject
    -&gt;src
        -&gt;awsomeProject
            -&gt;configuration.go
            -&gt;main.go

Configuration file has a simple structure just for demo:
<br>configuration.go:

package main

type Config struct {
    Data int
}

Main file just uses the Config struct:
<br>main.go

package main

import &quot;fmt&quot;

func main(){
    var cfg Config
    cfg.Data = 1
    fmt.Println(&quot;lalala&quot;)
}

The error that I have is:

> /usr/local/go/bin/go run /Users/lapetre/Work/awsomeProject/src/awsomeProject/main.go
command-line-arguments
src/awsomeProject/main.go:6: undefined: Config
Process finished with exit code 2

Any idea why the Config is not seen in main?
<br>Thanks

答案1

得分: 1

当您构建可重用的代码片段时,您将开发一个作为共享库的包。但是当您开发可执行程序时,您将使用“main”包将包作为可执行程序。包“main”告诉Go编译器该包应该编译为可执行程序,而不是共享库。包“main”中的main函数将是我们可执行程序的入口点。

这就是为什么您应该使用以下结构:

awsomeProject
    ->src
        ->awsomeProject
            ->configuration.go
            ->main.go

其中 main.go 的内容如下:

package main

import "fmt"

func main(){
    var cfg awsomeProject.Config
    cfg.Data = 1
    fmt.Println("lalala")
}

以及 configuration.go 的内容如下:

package awsomeProject

type Config struct {
    Data int
}

更多详情请参考:

英文:

When you build reusable pieces of code, you will develop a package as a shared library. But when you develop executable programs, you will use the package “main” for making the package as an executable program. The package “main” tells the Go compiler that the package should compile as an executable program instead of a shared library. The main function in the package “main” will be the entry point of our executable program.

That's why you should use the following structure:

awsomeProject
    -&gt;src
        -&gt;awsomeProject
            -&gt;configuration.go
            -&gt;main.go

with main.go

package main

import &quot;fmt&quot;

func main(){
    var cfg awsomeProject.Config
    cfg.Data = 1
    fmt.Println(&quot;lalala&quot;)
}

and configuration.go

package awsomeProject

type Config struct {
    Data int
}

For more details:

答案2

得分: 1

你是如何调用go run的?如果你像这样调用它:

go run main.go

那么这就是问题所在。

go run只会运行你告诉它运行的文件。所以你需要告诉它也运行configuration.go,或者如果你有多个go文件要运行,你可以使用

go run *.go

就像eXMoor建议的那样。

然而,"go run *.go" 有一些限制/缺点,所以更好的选择是使用go build而不是go run。

go build

会编译所有文件。然后,要运行可执行文件:

./awesomeProject

为了将所有这些组合成一个命令,可以编译和运行你正在开发的任何应用程序,你可以使用:

go build && ./${PWD##*/}

我将其设置为别名

gorunapp

只是为了方便一些。

这可能并不是你遇到的问题的答案,但希望你会发现这些信息有用。

英文:

How are you calling go run? If you're calling it like

go run main.go

then that's the problem.

Go run only runs the file(s) you tell it to. So you need to tell it to also run configuration.go, or if you have several go files to run you can use

go run *.go

as eXMoor suggested.

There are some limits/drawbacks to "go run *.go" however, so the better alternative is to use go build instead of go run.

go build

Will compile everything. Then, to run the executable:

./awesomeProject

To combine all of this into one command that will compile and run whatever app you're working on, you can use:

go build &amp;&amp; ./${PWD##*/}

I have it aliased to

gorunapp

just to make it easier.

This may not actually be the answer to the problem you're having, but hopefully you'll find it useful information regardless.

答案3

得分: 0

尝试使用以下命令运行你的应用程序:

go run *.go
英文:

Try to run your application with command:

go run *.go

huangapple
  • 本文由 发表于 2017年7月21日 19:20:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/45236350.html
匿名

发表评论

匿名网友

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

确定