LoginSignup
2

More than 1 year has passed since last update.

M1MacでGo/Ginの環境を作った過程

Posted at

はじめに

もともとWindowsユーザーだったけど、2022年01月からM1Macに変えました。
ただでさえ新しいガジェット(Mac)なのに、最新チップ(M1)でやったことない言語(Go)のさっき知ったフレームワーク(Gin)の環境を準備しようとしているんだから大変に決まってるよね。
だって記事がないんだもの。

そして、この記事はただの記録用です。
自分がこの後沼にハマる(確信)時に何をしたかを覚えておくためです。

だからこの記事正しくないと思うよ!

諸々のバージョン

Go: 1.18
OS: MacOS
チップ: M1

Goのダウンロード&インストール

(やめればいいのに)最新verをインストールしました。
go1.18.darwin-arm64.pkg
です。

スクリーンショット 2022-04-11 1.01.07.png

ダウンロードしたものを開いて、ひたすらYes押してったらインストールできました。

GOROOT&GOPATHの設定

これ打った。

terminal
$ export GOPATH=$HOME/Go
$ export GOROOT=/usr/local/go

けど、なんかターミナル再起動されるたびにリセットされた。
だから.bash_profileに記述しました。

.bash_profile
・
・
export GOPATH="$HOME/Go"
export GOROOT="/usr/local/go"

Ginのインストール

さぁ、こっからワケワカメ。

この方のサイトを参考にインストールしようとしたのですが、さっそくエラー。
なんかgo getコマンドはGoのバージョン1.17から非推奨になったらしい。
だからgo installコマンドでインストール

go install github.com/gin-gonic/gin@latest

んで、ファイル作った。

main.go
package main

import (
    "github.com/gin-gonic/gin"
)

func main() {
    router := gin.Default()
    router.LoadHTMLGlob("templates/*.html")

    router.GET("/", func(ctx *gin.Context){
        ctx.HTML(200, "index.html", gin.H{})
    })

    router.Run()
}
templates/index.html
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Sample App</title>
</head>
<body>
    <h1>Hello World!!!</h1>
</body>
</html>

コマンド実行

terminal
$ go run main.go
main.go:4:2: no required module provides package github.com/gin-gonic/gin: go.mod file not found in current directory or any parent directory; see 'go help modules'

エラーやないかい!
なんかモジュールがないらしい。
そしたらgo mod initってコマンドを打てばいいらしい。

terminal
$ go mod init
go: cannot determine module path for source directory /Users/ayumu-1212/Documents/private/programing/Go (outside GOPATH, module path must be specified)

Example usage:
        'go mod init example.com/m' to initialize a v0 or v1 module
        'go mod init example.com/m/v2' to initialize a v2 module

Run 'go help mod init' for more information.

エラーやないかい!
これの解決方法がわからんかった。。
そしたらなんか、そもそもGoってsrcとかpkgとかを$GOPATH直下に置かないといけないらしい。(しるか)
なので、$GOPATH直下にsrcを作って、その中にフォルダを作って(GinTutorialなど)、その中にさっきのファイルをいれた。
そしてもう一度コマンドうってみた。

terminal
$ go mod init
go: creating new go.mod: module GinTutorial
go: to add module requirements and sums:
        go mod tidy

え、初めて上手く行ったきがする。さぁもう一回run

terminal
$ go run main.go
main.go:4:2: no required module provides package github.com/gin-gonic/gin; to add it:
        go get github.com/gin-gonic/gin

$ go get github.com/gin-gonic/gin
go: downloading github.com/json-iterator/go v1.1.9
go: downloading github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: downloading github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: added github.com/gin-contrib/sse v0.1.0
go: added github.com/gin-gonic/gin v1.7.7
go: added github.com/go-playground/locales v0.13.0
go: added github.com/go-playground/universal-translator v0.17.0
go: added github.com/go-playground/validator/v10 v10.4.1
go: added github.com/golang/protobuf v1.3.3
go: added github.com/json-iterator/go v1.1.9
go: added github.com/leodido/go-urn v1.2.0
go: added github.com/mattn/go-isatty v0.0.12
go: added github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421
go: added github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742
go: added github.com/ugorji/go/codec v1.1.7
go: added golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
go: added golang.org/x/sys v0.0.0-20200116001909-b77594299b42
go: added gopkg.in/yaml.v2 v2.2.8

$ go run main.go
# golang.org/x/sys/unix
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/syscall_darwin.1_13.go:25:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.1_13.go:27:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.1_13.go:40:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:28:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:43:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:59:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:75:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:90:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:105:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:121:3: //go:linkname must refer to declared function or variable
../../pkg/mod/golang.org/x/sys@v0.0.0-20200116001909-b77594299b42/unix/zsyscall_darwin_arm64.go:121:3: too many errors

、、、キレそう。。
調べてみると、なんかsysのバージョン問題らしい。
アップグレードしてから、再度run

terminal
$ go get -u golang.org/x/sys
go: downloading golang.org/x/sys v0.0.0-20220408201424-a24fb2fb8a0f
go: upgraded golang.org/x/sys v0.0.0-20200116001909-b77594299b42 => v0.0.0-20220408201424-a24fb2fb8a0f

$ go run main.go            
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] Loaded HTML Templates (2): 
        - 
        - index.html

[GIN-debug] GET    /                         --> main.main.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Environment variable PORT is undefined. Using port :8080 by default
[GIN-debug] Listening and serving HTTP on :8080

おおおお、、、やっと来たか、、?
http://localhost:8080/ を起動。

スクリーンショット 2022-04-11 2.11.13.png

おーーーきたぁ!
やっと動いた、長いわ。

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
2