Mbded
Mbded

Reputation: 1916

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

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.

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

Upvotes: 15

Views: 8246

Answers (5)

Shiny
Shiny

Reputation: 1

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.

Upvotes: 0

Mbded
Mbded

Reputation: 1916

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

enter image description here

  • 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

enter image description here

  • Chose early defined Go Application my is Unamed
  • Click Play
  • An Joy :D

Upvotes: 31

user3814357
user3814357

Reputation: 1

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.

Upvotes: 0

Squareme
Squareme

Reputation: 89

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

Upvotes: 6

Bayu Cahya
Bayu Cahya

Reputation: 1

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.

Upvotes: -1

Related Questions