删除^M
windows保存的换行^M
This commit is contained in:
122
3.2.md
122
3.2.md
@@ -1,53 +1,53 @@
|
||||
#3.2 GO搭建一个web服务器
|
||||
|
||||
前面小节已经介绍了Web是基于http协议的一个服务,Go语言里面提供了一个完善的net/http包,通过http包可以很方便的就搭建起来一个可以运行的web服务。同时使用这个包能很简单地对web的路由,静态文件,模版,cookie等数据进行设置和操作。
|
||||
|
||||
##http包建立web服务器
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"log"
|
||||
)
|
||||
|
||||
func sayhelloName(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm() //解析参数,默认是不会解析的
|
||||
fmt.Println(r.Form) //这些信息是输出到服务器端的打印信息
|
||||
fmt.Println("path", r.URL.Path)
|
||||
fmt.Println("scheme", r.URL.Scheme)
|
||||
fmt.Println(r.Form["url_long"])
|
||||
for k, v := range r.Form {
|
||||
fmt.Println("key:", k)
|
||||
fmt.Println("val:", strings.Join(v, ""))
|
||||
}
|
||||
fmt.Fprintf(w, "Hello astaxie!") //这个写入到w的是输出到客户端的
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", sayhelloName) //设置访问的路由
|
||||
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
上面这个代码,我们build之后,然后执行web.exe,这个时候其实已经在9090端口监听tcp链接请求了。
|
||||
|
||||
在浏览器输入`http://localhost:9090`
|
||||
|
||||
可以看到浏览器页面输出了`Hello astaxie!`
|
||||
|
||||
可以换一个地址试试:`http://localhost:9090/?url_long=111&url_long=222`
|
||||
|
||||
看看浏览器输出的是什么,服务器输出的是什么?
|
||||
|
||||
在服务器端输出的信息如下:
|
||||
|
||||

|
||||
|
||||
#3.2 GO搭建一个web服务器
|
||||
|
||||
前面小节已经介绍了Web是基于http协议的一个服务,Go语言里面提供了一个完善的net/http包,通过http包可以很方便的就搭建起来一个可以运行的web服务。同时使用这个包能很简单地对web的路由,静态文件,模版,cookie等数据进行设置和操作。
|
||||
|
||||
##http包建立web服务器
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"log"
|
||||
)
|
||||
|
||||
func sayhelloName(w http.ResponseWriter, r *http.Request) {
|
||||
r.ParseForm() //解析参数,默认是不会解析的
|
||||
fmt.Println(r.Form) //这些信息是输出到服务器端的打印信息
|
||||
fmt.Println("path", r.URL.Path)
|
||||
fmt.Println("scheme", r.URL.Scheme)
|
||||
fmt.Println(r.Form["url_long"])
|
||||
for k, v := range r.Form {
|
||||
fmt.Println("key:", k)
|
||||
fmt.Println("val:", strings.Join(v, ""))
|
||||
}
|
||||
fmt.Fprintf(w, "Hello astaxie!") //这个写入到w的是输出到客户端的
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", sayhelloName) //设置访问的路由
|
||||
err := http.ListenAndServe(":9090", nil) //设置监听的端口
|
||||
if err != nil {
|
||||
log.Fatal("ListenAndServe: ", err)
|
||||
}
|
||||
}
|
||||
|
||||
上面这个代码,我们build之后,然后执行web.exe,这个时候其实已经在9090端口监听tcp链接请求了。
|
||||
|
||||
在浏览器输入`http://localhost:9090`
|
||||
|
||||
可以看到浏览器页面输出了`Hello astaxie!`
|
||||
|
||||
可以换一个地址试试:`http://localhost:9090/?url_long=111&url_long=222`
|
||||
|
||||
看看浏览器输出的是什么,服务器输出的是什么?
|
||||
|
||||
在服务器端输出的信息如下:
|
||||
|
||||

|
||||
|
||||
我们看到上面的代码,要编写一个web服务器很简单,只要调用http包的两个函数就可以了。
|
||||
|
||||
>- 如果你以前是PHP程序员,那你也许就会问,我们的nginx、apache服务器不需要吗?Go就是不需要这些,因为他直接就监听tcp端口了,做了nginx做的事情,然后sayhelloName这个其实就是我们写的逻辑函数了,也就是php里面的执行逻辑类似。
|
||||
@@ -58,14 +58,14 @@
|
||||
|
||||
我们看到Go通过简单的几行代码就已经运行起来一个web服务了,而且这个Web服务内部已经支持了高并发的特性,我将会在接下来的两个小节里面详细的讲解一下go是如何实现Web高并发的。
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一节: [Web工作方式](<3.1.md>)
|
||||
* 下一节: [Go如何使得web工作](<3.3.md>)
|
||||
|
||||
## LastModified
|
||||
* $Id$
|
||||
|
||||
|
||||
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一节: [Web工作方式](<3.1.md>)
|
||||
* 下一节: [Go如何使得web工作](<3.3.md>)
|
||||
|
||||
## LastModified
|
||||
* $Id$
|
||||
|
||||
Reference in New Issue
Block a user