You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mac下Brew安装Go后GoLand编译异常,如何配置正确路径?

Let's break this down and fix both your output issue and GoLand configuration step by step:

1. Fix the Missing Output (Your Code Issue)

First, the reason you're not seeing the expected "Hello" and "1+1 = 2" output is two problems in your code:

  • Go requires the main executable package to be named main (not HelloWorldProject)
  • fmt.Printf needs a format placeholder for your integer value—without it, the 1+1 argument gets ignored.

Here's the corrected code:

package main
import "fmt"

func main(){
 fmt.Println("Hello") // Println adds a newline automatically for readability
 fmt.Printf("1+1 = %d\n", 1+1) // %d is the placeholder for integer values
}

2. Confirm GOROOT Configuration in GoLand

Your Homebrew-installed Go has a GOROOT at /usr/local/Cellar/go/1.10/libexec, which matches what GoLand is showing. To make sure this is correctly set:

  • Open GoLand, go to GoLand > Settings (or Preferences on older Mac versions)
  • Navigate to Go > GOROOT
  • Ensure the path /usr/local/Cellar/go/1.10/libexec is selected and marked as the default. If it's not listed, click the + button to add it manually.

3. Configure GOPATH (System & GoLand)

Your current GOPATH (/Users/jeanmac/go) aligns with Homebrew's default recommendation, but let's make sure it's consistent across your system and GoLand:

System Environment Variables

  • Open your shell config file (e.g., ~/.zshrc if you use Zsh, ~/.bash_profile for Bash)
  • Add these lines to set GOPATH and update your PATH:
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin:/usr/local/opt/go/libexec/bin
  • Save the file, then run source ~/.zshrc (or your corresponding config file) to apply changes immediately.
  • Verify with go env in your terminal—check that GOROOT and GOPATH match the values we've set.

GoLand GOPATH Setup

  • In GoLand's Settings > Go > GOPATH, check that /Users/jeanmac/go is listed under Project GOPATH. GoLand usually picks up system environment variables automatically, but if not, click + to add it.

4. Validate Your Project Structure

You already have the correct structure: your code lives in $GOPATH/src/github.com/jpere547/HelloWorldProject—this follows Go's required workspace layout, so no changes needed here.

5. Test the Fix

After updating your code and verifying configurations, run the project again in GoLand. You should see this output in the console:

Hello
1+1 = 2

内容的提问来源于stack exchange,提问作者JeanP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:57:55