在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

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

GO in IntelliJ IDEA. Multiple File and Error Undefined: Data

问题

我想使用IntelliJ IDE Community Edition来编写GO(GoLang)代码。我安装了正确的插件,并安装了构建应用程序所需的所有工具。
我的应用程序由以下两个文件组成。每个文件都在目录*../EventServer*中。

  • Main.go
  • Data.go

如果我想从IntelliJ中使用运行功能(Ctlr+Shift+F10)运行项目,我会得到以下错误

/usr/lib/go/bin/go build -o "/tmp/Build Main.go and run0go" -gcflags "-N -l" /my/home/blah/EventServer/Main.go
# command-line-arguments
./Main.go:11: undefined: Data

我可以在终端中编译代码并进入项目目录执行命令,没有任何问题

:~/Pulpit/EventServer$ go build
./EventServer 
Hello
dane w strukturze someone

目录和文件树如下所示

EventServer$ tree -a
.
├── Data.go
├── EventServer
├── EventServer.iml
├── .idea
│   ├── compiler.xml
│   ├── copyright
│   │   └── profiles_settings.xml
│   ├── libraries
│   │   └── GOPATH__EventServer_.xml
│   ├── misc.xml
│   ├── modules.xml
│   ├── .name
│   ├── vcs.xml
│   └── workspace.xml
└── Main.go

我认为运行的命令有问题,因为编译器尝试只使用一个文件Main.go构建程序,而不是使用所有文件。正确的命令应该是

$ go run *.go 

但我不知道在哪里可以设置这个。

我还将GOPATH设置为:

export GOPATH=$HOME/Pulpit/EventServer

这也没有帮助

代码

Main.go

package main

import (
	"fmt"

)

func main() {

	fmt.Println("Hello")
	abcd := Data{"someone" , "1234567"}
	fmt.Printf("dane w strukturze %s ", abcd.Name)

}

Data.go

package main

type Data struct {
Name string
Phone string

}

系统:LINUX

英文:

I want use IntelliJ IDE Community Edition to write code in GO (GoLang). I instaled right plugin, and instaled all need tools to build application.
My application consists with two below file. Each is in direcytory ../EventServer.

  • Main.go
  • Data.go

If I want to run project from IntelliJ using function Run (Ctlr+Shift+F10) and I get below error

/usr/lib/go/bin/go build -o "/tmp/Build Main.go and run0go" -gcflags "-N -l" /my/home/blah/EventServer/Main.go
# command-line-arguments
./Main.go:11: undefined: Data

I can without any problem compiled code from terminal come in to direcytory with project and execution command

:~/Pulpit/EventServer$ go build
./EventServer 
Hello
dane w strukturze someone

tree direcytory and files looks like

EventServer$ tree -a
.
├── Data.go
├── EventServer
├── EventServer.iml
├── .idea
│   ├── compiler.xml
│   ├── copyright
│   │   └── profiles_settings.xml
│   ├── libraries
│   │   └── GOPATH__EventServer_.xml
│   ├── misc.xml
│   ├── modules.xml
│   ├── .name
│   ├── vcs.xml
│   └── workspace.xml
└── Main.go

I suppose that command to run is bad, because compiler trying build program with only one file Main.go but not with all files. Right command should be

$ go run *.go 

But I do not know where Can I set this.

I also set GOPATH to:

export GOPATH=$HOME/Pulpit/EventServer

This also hasn't help

CODE

Main.go

package main

import (
	"fmt"

)

func main() {

	fmt.Println("Hello")
	abcd := Data{"someone" , "1234567"}
	fmt.Printf("dane w strukturze %s ", abcd.Name)

}

And Data.go

package main

type Data struct {
Name string
Phone string

}

SYSTEM: LINUX

答案1

得分: 31

----------------------已解决-------------------------------------------已解决---------------------

步骤

  • 项目必须位于目录 for/example/MyProject/src/HERE_DIRECTORY_WITH_YOUR_SOURCE_GO_FILE
    子目录 src 很重要
  • 进入 Run --> Edit Configurations
  • 找到下面的位置

在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

  • Run Kind 更改为 Package
  • 在位置 Package 中写入您的代码所在的文件夹(应该会高亮显示,表示正确)
  • 点击左上角的加号图标,添加 Go Application
  • 应用更改
  • 在主窗口 IDE 的右上角,您会看到一个小图标 Play

在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

  • 选择之前定义的 Go Application,我的是 Unamed

  • 点击 Play

  • 开始享受吧 在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

英文:

----------------------SOLVED-------------------------------------------SOLVED---------------------

Steps

  • Project must be found in directory for/example/MyProject/src/HERE_DIRECTORY_WITH_YOUR_SOURCE_GO_FILE
    sub direcytory src is important
  • Go to Run --> Edit Configurations
  • Find below position

在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

  • Change Run Kind to Package
  • In Position Package write Your folder with Your code (Shold be Highlight it is correct)
  • Click On PLUS icon in left top corner, and Add Go Application
  • Apply changes
  • In the right top corner main window IDE You see small icon Play

在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

  • Chose early defined Go Application my is Unamed
  • Click Play
  • An Joy 在IntelliJ IDEA中使用GO。多个文件和错误未定义:Data

答案2

得分: 6

假设你有一个带有src/子目录的项目,并且其中有两个.go文件:hello.go和typetest.go,它们都定义了相同的包"main"。其中一个文件(hello.go)还实现了func main()。

为了使其作为一个整体编译,你需要确保两件事情配置正确:GOPATH和运行/调试配置。

打开项目库/GOPATH设置:

对于Gogland

文件 -> 设置 -> Go

对于Intellij IDEA

文件 -> 设置 -> 语言和框架 -> Go -> Go库

确保项目的GOPATH看起来像这样:

GOPATH设置

接下来,打开运行 -> 编辑配置,确保你的配置看起来类似于这样:

运行/调试配置

英文:

Let say you are having a project with src/ sub directory and two .go files inside: hello.go and typetest.go and both defines the same package "main". One of them (hello.go) also implements func main().

To make it compile as whole you need to make sure two things are configured properly: GOPATH and Run/Debug configuration.

Open the Project Libraries/GOPATH settings:

For Gogland

File -> Settings -> Go

For Intellij IDEA

File -> Settings -> Languages & Frameworks -> Go -> Go Libraries

Makes sure the GOPATH for the project looks something like that:

GOPATH settings

Next, open Run -> Edit Configurations and make sure your configuration looks similar to this:

Run/Debug configuration

答案3

得分: 0

你可以将第二个文件(在你的情况下是Data.go)添加为Go工具参数字段在运行/调试配置中。我在这里阅读到了相关信息:https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2013 讨论的最后。下面的示例(我使用了两个文件HelGo.go和aaa.go)对我有效:

带有2个文件的Go运行配置

Intellij在这两个文件中提出了异议,因为它们位于不同的文件夹中,所以它们都必须是相同的包(main)。
另一方面,我无法使这个页面上的任何建议起作用。

英文:

You can add second file (in your case Data.go) as Go tool arguments field in Run/Debug Configurations. I read about this here: https://github.com/go-lang-plugin-org/go-lang-idea-plugin/issues/2013 near the end of discussion. Example below (I used 2 files HelGo.go and aaa.go) worked for me:

Go Run Configuration with 2 files

Intellij objected in these 2 files were in different folders, and so both of them has to be same package (main).
On the other side I couldn't make any advises on this page work at all.

答案4

得分: 0

我使用一个完整的检查列表解决了相同的问题。通常,多个问题会相互结合。

首先,检查你的GOPATH设置,有两种方法:

  1. 使用命令行检查你的go环境:

     $ go env
    

    你将得到一个全局环境设置,请确保你的GO文件夹是正确的。如果你正在使用一些本地包,请注意它们。

  2. 检查你的构建工具设置,确保你已经将所有的资源文件添加到你的依赖中。这是设置项目GOPATH的另一种方式,不会影响全局设置。

其次,检查你的运行/调试配置,并确保你的设置已经定位到main包main文件。无论你使用哪种配置,这总是起点。

英文:

I solved the same problem with a complete list of checking. Usually, there are multiple problems combined with each other.

First of all, check your GOPATH settings, there are two ways:

  1. use the command line to check your go environment:

    $ go env
    

    you will get a global environment setting, make sure your GO folders are correct. If you are using some local packages, be aware of them too.

  2. check your Build tool setting, whether you have added all your resources files into your dependencies. This is another way to set up your project GOPATH, and do not affects the global settings.

Secondly, check your Run/Debug configuration, and make sure your settings have located the main package or main file. No matter which kind of configuration you use, this is always the start.

答案5

得分: -1

Sarp Kaya,请按照Mbded的步骤进行操作。额外的步骤是确保你的额外GOPATH已经设置好。

例如,这是我们的~/.profile GOPATH

export GOPATH=$HOME/lib/Go:$HOME/Development/Go

第一个路径用于go get等进程,而你的活动go开发目录则位于下一个路径。

根据我们的配置,RightApp的确切路径应该是$HOME/Development/Go/src/RightApp

英文:

Sarp Kaya, just follow Mbded steps. Additional step is, ensure that your additional GOPATH should be there.

For example, this is our ~/.profile GOPATH

export GOPATH=$HOME/lib/Go:$HOME/Development/Go

The first path used by go get processes etc, while your active go development directory goes to the next path.

According to our config, the RightApp exact path should be $HOME/Development/Go/src/RightApp.

huangapple
  • 本文由 发表于 2015年9月11日 01:31:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/32508254.html
匿名

发表评论

匿名网友

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

确定