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