[bigdata-087] ubuntu 16.04+linux+go 安装 编译 开发 beego+fasthttp web框架

来源:互联网 发布:盘锦兼职淘宝客服招聘 编辑:程序博客网 时间:2024/06/06 00:54
1. 官网
https://golang.org




2. 下载go安装包
https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz




3. 安装


3.1 sudo tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz 


3.2 修改/etc/profile增加path
    export PATH=$PATH:/usr/local/go/bin
    export GOPATH=/my/workspace/go


3.3 验证安装,在shell执行
    go version
    能看到版本信息既可




4. 下载liteide
https://downloads.sourceforge.net/project/liteide/X31/liteidex31.linux64-qt4.tar.bz2?r=https%3A%2F%2Fsourceforge.net%2Fprojects%2Fliteide%2F&ts=1496310731&use_mirror=jaist


然后解压缩到~/usr/liteide既可




5. 测试一个简单的go程序,在任意目录下创建一个hello.go文件,内容如下
------------------
package main


import "fmt"


func main() {
fmt.Printf("Hello, world.\n")
}
------------------
然后,在这里目录下编译,执行命令
go build
然后会生成一个跟当前目录一样的可执行文件,比如demo1,执行它,输出 Hello, world.




7. go的web框架


7.1 现在有37种go的web框架。这里有一个测试
https://github.com/smallnest/go-web-framework-benchmark


7.2 性能比较好的是https://github.com/valyala/fasthttp




8. fasthttp安装使用


8.1 安装
go get -u github.com/valyala/fasthttp
这个安装,本质上,是go get把相关包的源码下载到$GOPATH/src下,然后,如果新代码里引用了这里的源码,就会将此处源码一同编译,生成二进制结果。


8.2 在$GOPATH/src/test/fasthttp-demo1下创建文件x.go,内容如下
---------------------------
package main


import (
"flag"
"fmt"
"log"


"github.com/valyala/fasthttp"
)


var (
addr     = flag.String("addr", ":8080", "TCP address to listen to")
compress = flag.Bool("compress", false, "Whether to enable transparent response compression")
)


func main() {
flag.Parse()


h := requestHandler
if *compress {
h = fasthttp.CompressHandler(h)
}


if err := fasthttp.ListenAndServe(*addr, h); err != nil {
log.Fatalf("Error in ListenAndServe: %s", err)
}
}


func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Hello, world!\n\n")


fmt.Fprintf(ctx, "Request method is %q\n", ctx.Method())
fmt.Fprintf(ctx, "RequestURI is %q\n", ctx.RequestURI())
fmt.Fprintf(ctx, "Requested path is %q\n", ctx.Path())
fmt.Fprintf(ctx, "Host is %q\n", ctx.Host())
fmt.Fprintf(ctx, "Query string is %q\n", ctx.QueryArgs())
fmt.Fprintf(ctx, "User-Agent is %q\n", ctx.UserAgent())
fmt.Fprintf(ctx, "Connection has been established at %s\n", ctx.ConnTime())
fmt.Fprintf(ctx, "Request has been started at %s\n", ctx.Time())
fmt.Fprintf(ctx, "Serial request number for the current connection is %d\n", ctx.ConnRequestNum())
fmt.Fprintf(ctx, "Your ip is %q\n\n", ctx.RemoteIP())


fmt.Fprintf(ctx, "Raw request is:\n---CUT---\n%s\n---CUT---", &ctx.Request)


ctx.SetContentType("text/plain; charset=utf8")


// Set arbitrary headers
ctx.Response.Header.Set("X-My-Header", "my-header-value")


// Set cookies
var c fasthttp.Cookie
c.SetKey("cookie-name")
c.SetValue("cookie-value")
ctx.Response.Header.SetCookie(&c)
}
---------------------------
编译 go build
运行 ./fasthttp-demo1
然后在浏览器输入地址http://127.0.0.1:8080,看到helloworld等一大堆东东,表明安装成功。






9. beego


9.1 官网
https://beego.me/
https://github.com/astaxie/beego


9.2 安装--需要使用代理,否则无法下载
go get github.com/astaxie/beego
go get github.com/beego/bee 
这个安装,本质上,是go get把相关包的源码下载到$GOPATH/src下,然后,如果新代码里引用了这里的源码,就会将此处源码一同编译,生成二进制结果。


9.3 写一个例子测试beego
d1.go,内容如下:
---------------------------
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()

---------------------------
然后运行 go build -o hello d1.go
然后运行 ./hello
在浏览器输入地址http://127.0.0.1:8080/,既可看到"hello world",测试成功。





















阅读全文
0 0
原创粉丝点击