LoginSignup
6
3

More than 1 year has passed since last update.

M1 MacBook AirでGoを動かそうとしたらプチハマった

Posted at

 使用環境

  • MacBook Air M1
  • VSCode 1.63.2

導入目的

BEをGoで構築する案件にアサインされたので、お勉強をしようと思い環境導入です。

Go環境構築

  • ”Go Install”とか適当にググって出てきたサイトをポチポチクリックしてインストール。
  • VSCodeも勝手に”Goで書かれたコードやん!拡張機能入れたげるわ〜!”とサジェスト。

  • 続いてF5でデバッグ開始すると”Debuggerおらんやない〜? GoにおすすめのDelvインストールしとこか〜?”とマザーテレサ並みの献身。

あゝ、VSCode、君はなんてよくできたエディタなんだっ!
というふうに、最も簡単に環境構築はできたと思ったのでした。

ハマった

何も考えないで適当にHello World!
こんにちはGo!
はい、動きませんでした。

2fe9ba50-c15c-1f22-793c-42eb1a6b2445.png

Failed to launch: could not launch process: can not run under Rosetta, check that the installed build of Go is right for your CPU architecture

起動に失敗しました:プロセスを起動できませんでした:Rosettaで実行できません。インストールされているGoのビルドがCPUアーキテクチャに適していることを確認してください

Oh GOD

ディザスタリカバリ

Goのターミナルコマンドは使えるのか?

Terminalを立ち上げてGoコマンドが使えることをまず確認。

$ go
Go is a tool for managing Go source code.

Usage:

    go <command> [arguments]

The commands are:

    bug         start a bug report
    build       compile packages and dependencies
    clean       remove object files and cached files
    doc         show documentation for package or symbol
    env         print Go environment information
    fix         update packages to use new APIs
    fmt         gofmt (reformat) package sources
    generate    generate Go files by processing source
    get         add dependencies to current module and install them
    install     compile and install packages and dependencies
    list        list packages or modules
    mod         module maintenance
    run         compile and run Go program
    test        test packages
    tool        run specified go tool
    version     print Go version
    vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

    buildconstraint build constraints
    buildmode       build modes
    c               calling between Go and C
    cache           build and test caching
    environment     environment variables
    filetype        file types
    go.mod          the go.mod file
    gopath          GOPATH environment variable
    gopath-get      legacy GOPATH go get
    goproxy         module proxy protocol
    importpath      import path syntax
    modules         modules, module versions, and more
    module-get      module-aware go get
    module-auth     module authentication using go.sum
    packages        package lists and patterns
    private         configuration for downloading non-public code
    testflag        testing flags
    testfunc        testing functions
    vcs             controlling version control with GOVCS

Use "go help <topic>" for more information about that topic.

上記のようにGoに紐づいているコマンド一覧が表示できればOK。

 GoのVersionは?

Versionの確認。

$ Go version
go version go1.17.6 darwin/amd64

AMD64!!!
M1はARM64のはずです。

「”Go Install”とか適当にググって出てきたサイトをポチポチクリックしてインストール。」
しちゃだめ。

Goの削除と再インストール

下記記事を参考にして削除

再インストール

トップに出てくるMacOS用はレガシーなIntel CPU用

正しくは少し下にスクロールしたらあるARM用

Delveも再インストール

$ go install github.com/go-delve/delve/cmd/dlv@latest

この後再起動したらdlvがコマンドで通るようになります。

$ dlv
Delve is a source level debugger for Go programs.

Delve enables you to interact with your program by controlling the execution of the process,
evaluating variables, and providing information of thread / goroutine state, CPU register state and more.

The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.

Pass flags to the program you are debugging using `--`, for example:

`dlv exec ./hello -- server --config conf/config.toml`

Usage:
  dlv [command]

Available Commands:
  attach      Attach to running process and begin debugging.
  connect     Connect to a headless debug server.
  core        Examine a core dump.
  dap         [EXPERIMENTAL] Starts a headless TCP server communicating via Debug Adaptor Protocol (DAP).
  debug       Compile and begin debugging main package in current directory, or the package specified.
  exec        Execute a precompiled binary, and begin a debug session.
  help        Help about any command
  run         Deprecated command. Use 'debug' instead.
  test        Compile test binary and begin debugging program.
  trace       Compile and begin tracing program.
  version     Prints version.

Flags:
      --accept-multiclient               Allows a headless server to accept multiple client connections.
      --allow-non-terminal-interactive   Allows interactive sessions of Delve that don't have a terminal as stdin, stdout and stderr
      --api-version int                  Selects API version when headless. New clients should use v2. Can be reset via RPCServer.SetApiVersion. See Documentation/api/json-rpc/README.md. (default 1)
      --backend string                   Backend selection (see 'dlv help backend'). (default "default")
      --build-flags string               Build flags, to be passed to the compiler. For example: --build-flags="-tags=integration -mod=vendor -cover -v"
      --check-go-version                 Exits if the version of Go in use is not compatible (too old or too new) with the version of Delve. (default true)
      --disable-aslr                     Disables address space randomization
      --headless                         Run debug server only, in headless mode.
  -h, --help                             help for dlv
      --init string                      Init file, executed by the terminal client.
  -l, --listen string                    Debugging server listen address. (default "127.0.0.1:0")
      --log                              Enable debugging server logging.
      --log-dest string                  Writes logs to the specified file or file descriptor (see 'dlv help log').
      --log-output string                Comma separated list of components that should produce debug output (see 'dlv help log')
      --only-same-user                   Only connections from the same user that started this instance of Delve are allowed to connect. (default true)
  -r, --redirect stringArray             Specifies redirect rules for target process (see 'dlv help redirect')
      --wd string                        Working directory for running the program.

Additional help topics:
  dlv backend  Help about the --backend flag.
  dlv log      Help about logging flags.
  dlv redirect Help about file redirection.

Hello World再び

VSCodeでHello World書きます。

package main

import "fmt"

func main() {
    fmt.Print("hello world.")
}

Terminalでmodファイル作成。

$ go mod init main

ビルド実行で完成です。

その他

上記フォーラム読んでVSCodeも再インストールしましたがdlvが通ってなかっただけな気がしてます。
うまくいかなかった場合どうぞ。

結論

環境構築は丁寧に!

6
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
3