提示:本系列文章适合对Go有持续冲动的读者

初探golang web服务

golang web开发是其一项重要且有竞争力的应用,本小结来看看再golang中怎么创建一个简单的web服务。

在不适用web框架的情况下,可以使用net/http包搭建一个web服务。

  1. 这里我们使用net/http创建一个打印请求URL的web服务。
package main

import (
//"log"
"fmt"
"net/http"
) func main() {
http.HandleFunc("/", handler)
http.ListenAndServe("localhost:6677", nil) }
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "url.path=%q\n", r.URL.Path) //输出到文件流
}

http.HandleFunc函数可以理解为URL路由。

http.ListenAndServe是web服务的创建核心。

handler是http请求处理函数,接受一个http.ResponseWriter文件流 和http.Request类型的对象。

[root@VM-0-5-centos ~]# curl localhost:6677/123
url.path="/123"

  1. 我们通过handler函数来对访问url做访问数计算。

引入golang sync中的互斥锁,这样同时存在多个请求时只有一个goroutine改变count计数。互斥锁后续深入了解。

package main

import (
//"log"
"fmt"
"net/http"
"sync"
) var count int
var mutex sync.Mutex //使用互斥锁
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe("localhost:6677", nil) }
func handler(w http.ResponseWriter, r *http.Request) {
mutex.Lock()
count++
mutex.Unlock()
fmt.Fprintf(w, "request url.path:%q has %d times\n", r.URL.Path, count)
}

我们来看看请求结果如下:

[root@VM-0-5-centos ~]# curl localhost:6677/golang
request url.path:"/golang" has 1 times
[root@VM-0-5-centos ~]# curl localhost:6677/golang
request url.path:"/golang" has 2 times
[root@VM-0-5-centos ~]# curl localhost:6677/golang
request url.path:"/golang" has 3 times

  1. http.Request类型对象除了URL.Path属性外还有MethodProto等。我们通过handler函数分别打印出来。
package main

import (
//"log"
"fmt"
"net/http"
"sync"
) var count int
var mutex sync.Mutex //使用互斥锁
func main() {
http.HandleFunc("/", handler)
http.ListenAndServe("localhost:6677", nil) }
func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "%s,%s,%s,\n", r.Method, r.URL, r.Proto)
fmt.Fprintf(w, "host:%q\nremoteaddr:%q\n", r.Host, r.RemoteAddr)
for k, v := range r.Header {
fmt.Fprintf(w, "Header[%q]:%q\n", k, v)
}
for k, v := range r.Form {
fmt.Fprintf(w, "Form[%q]:%q\n", k, v)
} }

创建表单接受后输出如下:

//output
GET,/helloweb,HTTP/1.1,
host:"localhost:6677"
remoteaddr:"127.0.0.1:58088"
Header["User-Agent"]:["curl/7.29.0"]
Header["Accept"]:["*/*"]
Form[parm1]:hello
Form[parm2]:web

本次简单的了解了一下golang web服务,也是初尝章节结束。接下来会比较深入的学习golang的精彩细节与精华。


文章有不足的地方欢迎在评论区指出。

欢迎收藏、点赞、提问。关注顶级饮水机管理员,除了管烧热水,有时还做点别的。

最新文章

  1. windows 2008 R2 64位系统,找到Microsoft Excel 应用程序
  2. hadoop 8步走
  3. 【BZOJ 2005】[Noi2010]能量采集 (容斥原理| 欧拉筛+ 分块)
  4. 你必须知道的 34 个简单实用的 Ubuntu 快捷键
  5. twrp gui/actions.cpp 中的功能实现 tw_action ,tw_action_param ,第二章
  6. windows 下使用VMware Workstation Pro 工具,ubuntu创建虚拟机
  7. Mybatis认识
  8. SonarQube和Maven的集成
  9. POJ2385——Apple Catching
  10. Intellij IDEA 自动清除无效 import
  11. kettle中的合并记录使用记录
  12. mysql-5.7.10-winx64 绿色版安装办法
  13. 系统安装后的linux和vmware的网络配置
  14. js面向对象高级编程
  15. 实验吧—Web——WP之 FALSE
  16. redis实现异步任务队列
  17. json过滤某些属性 之@jsonignore
  18. 报表在vista和win7下无法浏览应用的解决办法
  19. linux安装memcached
  20. delphi加密算法

热门文章

  1. Redis持久化——AOF日志
  2. 分页系列之一:SQL Server 分页存储过程
  3. C#中普通缓存的使用
  4. 针对中国政府机构的准APT攻击样本Power Shell的ShellCode分析
  5. hdu4975 行列和构造矩阵(dp判断唯一性)
  6. POJ3160强连通+spfa最长路(不错)
  7. nodejs-REPL/回调函数/事件循环
  8. upload
  9. thinkphp中常用到的sql操作
  10. 【opencv】VideoCapture打不开本地视频文件或者网络IP摄像头