完成第三章的书写
This commit is contained in:
14
3.2.md
14
3.2.md
@@ -1,6 +1,6 @@
|
||||
#3.2 GO搭建一个web服务器
|
||||
|
||||
前面小节已经介绍了Web是基于http协议的一个服务,Go语言里面提供了一个完善的net/http包,通过http包可以很方便的就搭建起来一个可以运行的web服务。使用这个包能很简单地对web的路由,静态文件,模版,cookie等数据进行设置和操作。
|
||||
前面小节已经介绍了Web是基于http协议的一个服务,Go语言里面提供了一个完善的net/http包,通过http包可以很方便的就搭建起来一个可以运行的web服务。同时使用这个包能很简单地对web的路由,静态文件,模版,cookie等数据进行设置和操作。
|
||||
|
||||
##http包建立web服务器
|
||||
|
||||
@@ -10,10 +10,11 @@
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"log"
|
||||
)
|
||||
|
||||
func sayhelloName(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm()
|
||||
r.ParseForm() //解析参数,默认是不会解析的
|
||||
fmt.Println(r.Form) //这些信息是输出到服务器端的打印信息
|
||||
fmt.Println("path", r.URL.Path)
|
||||
fmt.Println("scheme", r.URL.Scheme)
|
||||
@@ -26,8 +27,11 @@
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", sayhelloName)
|
||||
http.ListenAndServe(":9090", nil)
|
||||
http.HandleFunc("/", sayhelloName) //设置访问的路由
|
||||
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
上面这个代码,我们build之后,然后执行web.exe,这个时候其实已经在9090端口监听tcp链接请求了。
|
||||
@@ -52,7 +56,7 @@
|
||||
|
||||
>- 如果你以前是ruby程序员,那么和ROR的/script/server启动有点类似。
|
||||
|
||||
我们看到我们通过简单的几行代码就已经运行起来一个web服务了,而且这个web服务内部已经支持了高并发的特性,我将会在接下来的两个小节里面详细的讲解一下go是如何实现web高并发的。
|
||||
我们看到Go通过简单的几行代码就已经运行起来一个web服务了,而且这个Web服务内部已经支持了高并发的特性,我将会在接下来的两个小节里面详细的讲解一下go是如何实现Web高并发的。
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user