使用JetBrains GoLand IDE运行简单的Go应用程序。

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

Run go simple app with Jetbrains GoLand IDE

问题

我刚开始学习GO。好的,我遇到了一些奇怪的问题,还没有找到解决方法。
我有两个go文件。一个是main.go,另一个是state.go,它们都在同一个名为main的包中。
在state.go文件中,我定义了一个简单的函数printHello,然后在main.go中调用它。

state.go

package main

import "fmt"

func printHello() {
    fmt.Println("Hello World!")
}

main.go

package main

func main()  {
    printHello()
}

当我在命令行中使用命令go run main.go state.go运行它时,它可以正常工作,但在GoLand IDE中却不行。我尝试了构建它,并将运行类型更改为目录,但没有成功。我还附上了一张图片以供更清楚地说明。
使用JetBrains GoLand IDE运行简单的Go应用程序。

英文:

I've just started learning GO. Ok, I've some weird issue and did not figure out how to fix it.
I have 2 go file. one is main.go and second is state.go and they are both in same package called main.
In state.go file I defined simple function printHello, which then I'm calling it in main.go.

state.go

package main

import "fmt"

func printHello() {
	fmt.Println("Hello World!")
}

main.go

package main

func main()  {
    printHello()
}

When I run it in cmd with command: go run main.go state.go it works fine, but in GoLand IDE it didn't. I tried to build it changed Run Kind to Directory, but without success. Also attached image for more clarification
使用JetBrains GoLand IDE运行简单的Go应用程序。

答案1

得分: 1

在编辑器中打开main.go文件。

点击主函数左侧的绿色箭头。

它将运行并创建一个可以自定义的运行配置。

英文:

With main.go opened in the editor.

Click on the green arrow at the left side of main function.

It will run and creates a run configuration that you can customize.

答案2

得分: 1

你不需要运行 state.go,只需运行 main.go。Goland IDE 会处理它们,因为它们在同一个包(main)中。

英文:

You don't have to run the state.go; just run main.go. The Goland IDE will take care of it since they are in the same package (main).

huangapple
  • 本文由 发表于 2021年10月8日 23:57:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/69498671.html
匿名

发表评论

匿名网友

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

确定