macos下golang配置
Write By CS逍遥剑仙
我的主页: csxiaoyao.com
GitHub: github.com/csxiaoyaojianxian
Email: sunjianfeng@csxiaoyao.com
QQ: 1724338257
1. 安装homebrew
终端输入
<code>ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"</code>
安装后更新
<code>brew update</code>
2. 安装go
<code>brew install go</code>
3. 配置环境变量
<code>export GOBIN="/usr/local/go/bin" export GOPATH="/Users/sunshine/gowork" export PATH="$PATH:$GOBIN:$GOPATH/bin"</code>
4. 创建工作目录及配置GOPATH
<code>vim ~/.bash_profile source ~/.bash_profile</code>
或 vim ~/.zshrc
<code>export GOPATH=/Users/sunshine/gowork</code>
5. 测试
<code>cd /Users/sunshine/gowork mkdir src pkg bin main cd main touch test.go</code>
写入代码
<code>package main import "fmt" func main() { fmt.Printf("Hello, world") } </code>
执行
<code>go run test.go</code>
输出 Hello, world 表示正常
6. 安装beego
需要安装 Beego 和 Bee 开发工具
<code>$ go get github.com/astaxie/beego $ go get github.com/beego/bee</code>
7. 测试
<code>$ cd $GOPATH/src $ bee new hello $ cd hello $ bee run hello</code>
Windows 平台下
<code>$ cd %GOPATH%/src $ bee new hello $ cd hello $ bee run hello</code>
浏览器中打开 http://localhost:8080/ 进行访问
8. 简单示例
浏览器中打印 “Hello world”
<code>package main import ( "github.com/astaxie/beego" ) type MainController struct { beego.Controller } func (this *MainController) Get() { this.Ctx.WriteString("hello world") } func main() { beego.Router("/", &MainController{}) beego.Run() }</code>
保存为 hello.go,命令行编译执行
<code>$ go build -o hello hello.go $ ./hello</code>
浏览器中打开 http://127.0.0.1:8080 ,返回 “hello world”。
9. 问题解决
找不到指令、bee安装失败
<code>cd ${GOPATH}/src/github.com/beego/bee</code>
版本回退并重新安装
<code>git reset --hard 69023e9ae0b0d65cc2394c791c5af777311a06d4 go install</code>