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(notHelloWorldProject) fmt.Printfneeds a format placeholder for your integer value—without it, the1+1argument 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(orPreferenceson older Mac versions) - Navigate to
Go > GOROOT - Ensure the path
/usr/local/Cellar/go/1.10/libexecis 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.,
~/.zshrcif you use Zsh,~/.bash_profilefor 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 envin your terminal—check thatGOROOTandGOPATHmatch the values we've set.
GoLand GOPATH Setup
- In GoLand's
Settings > Go > GOPATH, check that/Users/jeanmac/gois listed underProject 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

