博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go gin server初体验
阅读量:4143 次
发布时间:2019-05-25

本文共 3378 字,大约阅读时间需要 11 分钟。

     gin可用作go环境的web server, 很好用,来玩下:

 

     1.  go get github.com/gin-gonic/gin  下载并安装gin库

      结果出现:go build github.com/ugorji/go/codec: /usr/lib/go-1.6/pkg/tool/linux_amd64/compile: signal: killed

      在网上查了一下,是oom所致, 用dmesg确认了一下, 果然如此:

[114220.386131] [ 4027]   500  4027    20792      155      41       4        0             0 curl[114220.386132] Out of memory: Kill process 4018 (compile) score 542 or sacrifice child[114220.387049] Killed process 4018 (compile) total-vm:492476kB, anon-rss:478772kB, file-rss:0kB

       买的腾讯云机器, 内存小, 晕。

       索性重启ubuntu, 再go get github.com/gin-gonic/gin一下, 就OK了。

 

      2.  写服务s.go

package mainimport "github.com/gin-gonic/gin"func main() {        r := gin.Default()        r.GET("/ping", func(c *gin.Context) {                c.JSON(200, gin.H{                        "message": "pong",                })        })        r.Run() // listen and serve on 0.0.0.0:8080}

     go run s.go跑起来

      

    3. 发请求:

ubuntu@VM-0-15-ubuntu:~$ curl "http://localhost/ping"curl: (7) Failed to connect to localhost port 80: Connection refusedubuntu@VM-0-15-ubuntu:~$ ubuntu@VM-0-15-ubuntu:~$ curl "http://localhost:8080/ping"{"message":"pong"}ubuntu@VM-0-15-ubuntu:~$

      收到请求了。

      另外, 注意到:

ubuntu@VM-0-15-ubuntu:~/taoge/go$ go envGOARCH="amd64"GOBIN=""GOEXE=""GOHOSTARCH="amd64"GOHOSTOS="linux"GOOS="linux"GOPATH="/home/ubuntu/go"GORACE=""GOROOT="/usr/lib/go-1.6"GOTOOLDIR="/usr/lib/go-1.6/pkg/tool/linux_amd64"GO15VENDOREXPERIMENT="1"CC="gcc"GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0"CXX="g++"CGO_ENABLED="1"

      我们去GOPATH路径下看下:

ubuntu@VM-0-15-ubuntu:~/go/src/github.com/gin-gonic/gin$ pwd/home/ubuntu/go/src/github.com/gin-gonic/ginubuntu@VM-0-15-ubuntu:~/go/src/github.com/gin-gonic/gin$ lsauth.go             context_17_test.go    doc.go                   internal            README.md                routes_test.goAUTHORS.md          context_appengine.go  errors.go                LICENSE             recovery.go              testdataauth_test.go        context.go            errors_test.go           logger.go           recovery_test.go         test_helpers.goBENCHMARKS.md       context_test.go       examples                 logger_test.go      render                   tree.gobenchmarks_test.go  CONTRIBUTING.md       fs.go                    Makefile            response_writer_1.7.go   tree_test.gobinding             coverage.sh           gin.go                   middleware_test.go  response_writer_1.8.go   utils.goCHANGELOG.md        debug.go              gin_integration_test.go  mode.go             response_writer.go       utils_test.gocodecov.yml         debug_test.go         ginS                     mode_test.go        response_writer_test.go  vendorCODE_OF_CONDUCT.md  deprecated.go         gin_test.go              path.go             routergroup.go           wercker.ymlcontext_17.go       deprecated_test.go    githubapi_test.go        path_test.go        routergroup_test.goubuntu@VM-0-15-ubuntu:~/go/src/github.com/gin-gonic/gin$

      这就是gin对应的包所在的目录。

 

      gin server可以吐回单纯的文本串, 也自然能吐回html, 让浏览器来显示, s.go代码为:

package main import (    "github.com/gin-gonic/gin"    "net/http") func first(c *gin.Context) {    s := `        
` c.Header("Content-Type", "text/html; charset=utf-8") // 不可少 c.String(http.StatusOK, "%s", s)}func main() { r := gin.Default() r.GET("/", first) r.Run(":8080")}

      gin功能很强大,如上仅仅是简单demo. 

      不多说。

 

 

转载地址:http://dizti.baihongyu.com/

你可能感兴趣的文章
linux进程监控和自动重启的简单实现
查看>>
OpenFeign学习(三):OpenFeign配置生成代理对象
查看>>
OpenFeign学习(四):OpenFeign的方法同步请求执行
查看>>
OpenFeign学习(六):OpenFign进行表单提交参数或传输文件
查看>>
Ribbon 学习(二):Spring Cloud Ribbon 加载配置原理
查看>>
Ribbon 学习(三):RestTemplate 请求负载流程解析
查看>>
深入理解HashMap
查看>>
XML生成(一):DOM生成XML
查看>>
XML生成(三):JDOM生成
查看>>
Ubuntu Could not open lock file /var/lib/dpkg/lock - open (13:Permission denied)
查看>>
collect2: ld returned 1 exit status
查看>>
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>