Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use native Go and development environment for M1 Mac

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

This article shows you how M1 Mac uses native Go and development environment, the content is concise and easy to understand, it will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

Go version 1.16 will officially support Apple Silicon M1 chip, that is, the Mac operating system of arm64 architecture. At present, go version 1.16 version is beta1, but it will repair bug and improve documentation on this basis.

Currently, there are two ways to experience Go1.16 first:

Method 1: compile the source code

You need to ensure that rosetta 2 (https://support.apple.com/en-us/HT211861)) is installed on the mac.

To install Go1.15, developing the compiler of Go from 1.4 requires Go itself to compile Go, so in order to compile 1.16, we first need the amd64 version of Go1.15.

Of course, some tool chains need to be installed on Mac: of course, if MacOS has other problems after upgrading, such as: xcrun: error: invalid active developer path, missing xcrun

We also use the following command to solve the problem:

Xcode-select-- installsudo xcode-select-switch / mkdir $HOME/gorootscd $HOME/gorootswget https://dl.google.com/go/go1.15.6.darwin-amd64.tar.gztar xzf go1.15.6.darwin-amd64.tar.gz

Get the latest source code for Go. After clone comes out, the current master branch is the pre-release branch of 1.16.

Cd $HOME/gorootsgit clone https://go.googlesource.com/go gosource

To compile go, you need to precompile the bootstrap response toolchain, which itself needs to be arm64, otherwise the final make will be x86.

Cd $HOME/goroots/gosource/srcarch-- x86 million 64 env GOROOT_BOOTSTRAP=$HOME/goroots/go GODEBUG=asyncpreemptoff=1 GOOS=darwin GOARCH=arm64. / bootstrap.bash

Note that GODEBUG=asyncpreemptoff=1 is included in the above compilation environment variable parameters. This is because our go 1.15 is amd64 version, and we need to rely on rosetta 2 when running directly in M1 mac. Currently, it seems that rosetta 2 also has bug for asynchronous preemption of go programs, so it needs to be turned off; otherwise, it will deadlock and cause errors at compile time:

Rosetta error: ulock_wait failure: 105

Once this is done, we can get the arm64 toolchain that compiles the full go under the $HOME/goroots/go-darwin-arm64-bootstrap path.

The final step is to compile arm64 native gocd $HOME/goroots/gosource/srcarch-- arm64e env GOROOT_BOOTSTRAP=$HOME/goroots/go-darwin-arm64-bootstrap. / make.bashexport PATH= "$PATH:$HOME/goroots/gosource/bin/"

After that, it can be used normally.

Method 2: download Go1.16beta1

Https://golang.org/dl/go1.16beta1.darwin-arm64.pkg

After download and installation, you can directly use:

Go versiongo version go1.16beta1 darwin/arm64

Development tools

At present, Goland official version has been developed to support the development of Mac M1.

But at present, the tool chain of Goland can not carry out DEBUG normally.

Solution:

Git clone https://github.com/oxisto/delve.gitgit checkout-b darwin-arm64-lldbcd delve/cmd/dlv/ go build go installcd $GOPATHcp dlv/Applications/GoLand.app/Contents/plugins/go/lib/dlv/Mac/dlv

After compiling delve to arm64, overwrite the version on Goland, and you can use it.

Ok, so we can finally write and debug our framework code on M1 again.

The above is how M1 Mac uses native Go and development environment. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report