Golang(6)Web Basic

前端之家收集整理的这篇文章主要介绍了Golang(6)Web Basic前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_404_2@Golang(6)Web Basic

@H_404_2@3.1 How Go Work with Web
@H_404_2@URL (Uniform Request Locator)
@H_404_2@DNS (Domain Name System)
@H_404_2@TCP ——> HTTP (80 port TCP)

@H_404_2@Http Request
@H_404_2@Http Request —> Request Line,Request Header,Request Body

@H_404_2@GET /domains/example/ HTTP/1.1 //Request Line: Request Method
@H_404_2@Host: www.iana.org //server host name
@H_404_2@User-Agent
@H_404_2@Accept //Client mine
@H_404_2@Accept-Encoding //
@H_404_2@Accept-Charset //Client Charset

@H_404_2@Body

@H_404_2@Http Response
@H_404_2@HTTP/1.1 200 OK //response status Line
@H_404_2@Server: Nginx/1.0.8 //Web Server software name and version
@H_404_2@Date: Tue,30 Oct 2012 04:14:25 GMT //sending time
@H_404_2@Content-Type: text/html //Server side data mine
@H_404_2@Transfer-Encoding: chunked //
@H_404_2@Connection: keep-alive
@H_404_2@Content-Length: 90 //length of the body

@H_404_2@<!Document html>…. Body

@H_404_2@4XX client errors,5XX server side errors,2XX success,3XX redirect,1XX info

@H_404_2@Keep-Alive
@H_404_2@After HTTP/1.1,Keep-Alive will default be true. We can reuse the TCP connection. This Keep-Alive time can be set on the server side configuration.

@H_404_2@3.2 Construct the Web Server based on Go

@H_404_2@The Simplest Server based on http package
@H_404_2@package main

@H_404_2@import (
@H_404_2@ "fmt"
@H_404_2@ "log"
@H_404_2@ "net/http"
@H_404_2@ "strings"
@H_404_2@)

@H_404_2@func sayhelloName(w http.ResponseWriter,r *http.Request) {
@H_404_2@ r.ParseForm() //解析参数,默认是不会解析的
@H_404_2@ fmt.Println("form: ",r.Form) //这些信息是输出到服务器端的打印信息
@H_404_2@ fmt.Println("path: ",r.URL.Path)
@H_404_2@ fmt.Println("scheme: ",r.URL.Scheme)
@H_404_2@ fmt.Println(r.Form["url_long"])
@H_404_2@ for k,v := range r.Form {
@H_404_2@ fmt.Println("key:",k)
@H_404_2@ fmt.Println("val:",strings.Join(v,""))
@H_404_2@ }
@H_404_2@ fmt.Fprintf(w,"Hello astaxie!") //这个写入到w的是输出到客户端的
@H_404_2@}

@H_404_2@func main() {
@H_404_2@ http.HandleFunc("/",sayhelloName) //设置访问的路由
@H_404_2@ err := http.ListenAndServe(":9090",nil) //设置监听的端口
@H_404_2@ if err != nil {
@H_404_2@ log.Fatal("ListenAndServe: ",err)
@H_404_2@ }
@H_404_2@}

@H_404_2@Visit the http://localhost:9090 to see the result.

@H_404_2@3.3 Further Work to Build Web
@H_404_2@Some Concepts from Server Side
@H_404_2@Request,Response,Connection,Handler

@H_404_2@How http Package Works
@H_404_2@for {
@H_404_2@ rw,e := l.Accept()
@H_404_2@ if e != nil {
@H_404_2@ }
@H_404_2@ c,err := srv.newConn(rw)
@H_404_2@ if err != nil {
@H_404_2@ continue
@H_404_2@ }
@H_404_2@ go c.serve()
@H_404_2@}

@H_404_2@handler is the second parameter of ListenAndServe,if it is nil,we will use handler = DefaultServeMux

@H_404_2@3.4 Details of http Package
@H_404_2@There are 2 key functions in http: Conn,ServeMux

@H_404_2@Conn and Concurrence
@H_404_2@c,err := srv.newConn(rw)
@H_404_2@if err != nil {
@H_404_2@ continue
@H_404_2@}
@H_404_2@go c.serve()

@H_404_2@Customer ServeMux
@H_404_2@How the old codes work

@H_404_2@type ServeMux struct {
@H_404_2@ mu sync.RWMutex //lock
@H_404_2@ m map[string]muxEntry
@H_404_2@ hosts bool // if put the host in the mapping rules
@H_404_2@}

@H_404_2@type muxEntry struct {
@H_404_2@ explicit bool //match exactly
@H_404_2@ h Handler
@H_404_2@ pattern string
@H_404_2@}

@H_404_2@type Handler interface {
@H_404_2@ ServeHTTP(ResponseWriter,*Request)
@H_404_2@}

@H_404_2@When we call method HandlerFuc,it will convert the function to contains method ServHTTP

@H_404_2@type HandlerFunc func(ResponseWriter,*Request)

@H_404_2@func (f HandlerFunc) ServeHTTP(w ResponseWriter,r *Request) {
@H_404_2@ f(w,r)
@H_404_2@}

@H_404_2@Map the Rules and then Call the Handler

@H_404_2@Customer Mapping
@H_404_2@package main

@H_404_2@import (
@H_404_2@ "fmt"
@H_404_2@ "net/http"
@H_404_2@)

@H_404_2@type MyMux struct {
@H_404_2@}

@H_404_2@func (p *MyMux) ServeHTTP(w http.ResponseWriter,r *http.Request) {
@H_404_2@ if r.URL.Path == "/" {
@H_404_2@ sayhelloName(w,r)
@H_404_2@ return
@H_404_2@ }
@H_404_2@ http.NotFound(w,r)
@H_404_2@ return
@H_404_2@}

@H_404_2@func sayhelloName(w http.ResponseWriter,r *http.Request) {
@H_404_2@ fmt.Fprintf(w,"Hello myroute!")
@H_404_2@}

@H_404_2@func main() {
@H_404_2@ mux := &MyMux{}
@H_404_2@ http.ListenAndServe(":9090",mux)
@H_404_2@}

@H_404_2@4. Forms
@H_404_2@…snip...

@H_404_2@References:
https://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/03.0.md

猜你在找的Go相关文章