加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

[bigdata-087] ubuntu 16.04+linux+go 安装 编译 开发 beego+fas

发布时间:2020-12-14 03:09:42 所属栏目:大数据 来源:网络整理
导读: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/g
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!nn") fmt.Fprintf(ctx,"Request method is %qn",ctx.Method()) fmt.Fprintf(ctx,"RequestURI is %qn",ctx.RequestURI()) fmt.Fprintf(ctx,"Requested path is %qn",ctx.Path()) fmt.Fprintf(ctx,"Host is %qn",ctx.Host()) fmt.Fprintf(ctx,"Query string is %qn",ctx.QueryArgs()) fmt.Fprintf(ctx,"User-Agent is %qn",ctx.UserAgent()) fmt.Fprintf(ctx,"Connection has been established at %sn",ctx.ConnTime()) fmt.Fprintf(ctx,"Request has been started at %sn",ctx.Time()) fmt.Fprintf(ctx,"Serial request number for the current connection is %dn",ctx.ConnRequestNum()) fmt.Fprintf(ctx,"Your ip is %qnn",ctx.RemoteIP()) fmt.Fprintf(ctx,"Raw request is:n---CUT---n%sn---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",测试成功。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读