macos下golang配置

macos下golang配置

1. 安装homebrew

终端输入

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

安装后更新

brew update

2. 安装go

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值