- 安装 BeeGo。在终端窗口中,输入命令安装 beego:
go get github.com/astaxie/beego
(如果要升级到新的版本,执行:go get -u github.com/astaxie/beego) - 打开 Sublime Text3,在 $GOPATH/src/web1 目录下($GOPATH 是你的 go 的源码目录,web1 是自己建立的一个目录),新建一个文件,并保存名字为:hello.go,然后在里面输入如下代码:
保存。package main import ( "github.com/astaxie/beego" ) type HomeController struct { beego.Controller } func (this *HomeController) Get() { this.Ctx.WriteString("hello world! 你好,世界!") } func main() { beego.Router("/", &HomeController{}) beego.Run() }
- 到命令行中,进入 $GOPATH/src/ 目录,并输入命令运行:
go run web1/hello.go
结果如图:
打开浏览器,访问:http://127.0.0.1:8080/,看到结果如下:
参考:http://www.ucai.cn/course/chapter/87/3267/4732