Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

  • 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

like image 662
Mbded Avatar asked Sep 10 '15 17:09

Mbded


People also ask

Can we use IntelliJ for Golang?

The Go functionality in IntelliJ IDEA is supported by the Go plugin. The Go plugin provides support of all the features that are available in GoLand, the standalone IDE for Go developers.

How do I get to the error line in IntelliJ?

Navigate between errors or warnings To jump to the next or previous found issue in your code, press F2 or Shift+F2 respectively. Alternatively, from the main menu, select Navigate | Next / Previous Highlighted Error. IntelliJ IDEA places the caret immediately before the code issue.


2 Answers

----------------------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
like image 178
Mbded Avatar answered Sep 18 '22 16:09

Mbded


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

like image 29
Squareme Avatar answered Sep 19 '22 16:09

Squareme