Cuong Huy
Cuong Huy

Reputation: 61

Intellij cannot load file .go same package

I have the problem running file main.go in Intellij.

My structure project

Main.go and Common.go same package main. I run Main.go in Intellij then consle log display message error: ".\Main.go:9: undefined: showMsg". showMsg is a function of Common.go

Upvotes: 3

Views: 560

Answers (3)

Stan
Stan

Reputation: 370

  1. Create a go.mod file in the root directory of your project with the following contents:
    module mypackage
    
  2. In your run configuration select "Run kind: Package" and "Package path: mypackage".

Feel free to change mypackage to your own package name.

Upvotes: 0

dlsniper
dlsniper

Reputation: 7477

This video should show you how to solve the problem. You basically need to use a Run Configuration of type Go Application and use a Package kind not File kind.

Upvotes: 3

syntagma
syntagma

Reputation: 24344

You are executing go run main.go from IntelliJ, so it doesn't look for showMsg() in other files.

You should execute go build instead, so that all of the files in the main package would be compiled into a single binary.

Upvotes: 0

Related Questions