1. 前言
计算机软件经历了数十年的发展,形成了多种学术流派,有面向过程编程、面向对象编程、函数式编程、面向消息编程等,这些思想究竟孰优孰劣,众说纷纭。
除了OOP外,近年出现了一些小众的编程哲学,Go语言对这些思想亦有所吸收。例如,Go语言接受了函数式编程的一些想法,支持匿名函数与闭包。再如,Go语言接受了以Erlang语言为代表的面向消息编程思想,支持goroutine和通道,并推荐使用消息而不是共享内存来进行并发编程。总体来说,Go语言是一个非常现代化的语言,精小但非常强大。
Go 语言最主要的特性:
- 自动垃圾回收
- 更丰富的内置类型
- 函数多返回值
- 错误处理
- 匿名函数和闭包
- 类型和接口
- 并发编程
- 反射
- 语言交互性
Web开发已成当今主流,Go开发社区已浮现了许多优秀的Web Framwork,本文通过3个sample简单介绍一下国人开发的Beego。
https://github.com/astaxie/beego
image.png
2. Beego安装
Download and install
- 国内可能会timeout,可以借用
proxychains4 shell完成
go get github.com/astaxie/beego
Create file hello.go
package main
import "github.com/astaxie/beego"
func main(){
beego.Run()
}
Build and run
go build hello.go
./hello
Go to http://localhost:8080
Congratulations! You've just built your first beego app.
3. Beego samples
WebIM - Chat room demo based on long polling and WebSocket.
image.png
Todo - todo app based on beego.angularJS with API which is designed by beego
image.png
shorturl - shouturl app based on beego. API applications
$ http :8080/v1/shorten/?longurl=http://google.com
HTTP/1.1 200 OK
Content-Length: 59
Content-Type: application/json; charset=utf-8
Date: Sun, 24 Nov 2019 14:21:21 GMT
Server: beegoServer:1.12.0
{
"UrlLong": "http://google.com",
"UrlShort": "5laZF"
}
$ http :8080/v1/expand/?shorturl=5laZF
HTTP/1.1 200 OK
Content-Length: 59
Content-Type: application/json; charset=utf-8
Date: Sun, 24 Nov 2019 14:21:50 GMT
Server: beegoServer:1.12.0
{
"UrlLong": "http://google.com",
"UrlShort": "5laZF"
}
4. Refers
- Go语言教程
https://www.runoob.com/go/go-concurrent.html - Beego简介
https://beego.me/docs/intro/ - Bee is a tool for helping develop with beego app framework
https://github.com/beego/bee - 无闻的video tutorial
https://study.163.com/course/courseMain.htm?courseId=328001












网友评论