在M1Mac上创建Go / Gin环境的过程

首先

我原本是Windows用户,但从2022年1月开始换成了M1Mac。
本来就是一个新的设备(Mac),而且还要准备使用从未接触过的编程语言(Go)和刚刚了解的框架(Gin)的环境,这当然会非常困难。
因为找不到相关的文章啊。

然后,这篇文章仅仅是为了记录而存在。
这是为了记住自己在之后陷入困境时所做的事情。

所以我认为这篇文章是不正确的!

各种版本

Go版本:1.18
操作系统:MacOS
芯片:M1

下载和安装Go语言

如果你停止的话,就好了。我安装了最新版本的go1.18.darwin-arm64.pkg。

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

打开下载的文件,只需一直点击“是”,就能完成安装。

设置GOROOT和GOPATH

我打了这个。

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

然而,每次终端重新启动都会重置它。
所以我将它写在了.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

然后,我创建了一个文件。

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()
}
<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <title>Sample App</title>
</head>
<body>
    <h1>Hello World!!!</h1>
</body>
</html>

执行命令

$ 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”命令。

$ 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之类的),把刚才的文件放进去。
然后再次输入命令试试看。

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

咦,感觉第一次运行得很顺利。好吧,再来一次运行吧。

$ 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的版本问题。
升级之后,再次运行一次试试看。

$ 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

哇哇哇!终于动了,好久啊。

广告
将在 10 秒后关闭
bannerAds