Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 日本a及毛片免费视频,国精品一区二区三区,国内精品一级毛片免费看

          整合營(yíng)銷服務(wù)商

          電腦端+手機(jī)端+微信端=數(shù)據(jù)同步管理

          免費(fèi)咨詢熱線:

          Golang Web編程,array數(shù)組、Slice切片、Map集合、Struct結(jié)構(gòu)體

          or rang語句 :實(shí)現(xiàn)遍歷數(shù)據(jù)功能,可以實(shí)現(xiàn)對(duì)數(shù)組、Slice、Map、或Channel等數(shù)據(jù)結(jié)構(gòu)

          . 表示每次迭代循環(huán)中的元素

          • 切片的循環(huán)

          main.go源碼及解析

          package main
          
          import (
          	"net/http"
          	"text/template"
          )
          
          func Index(w http.ResponseWriter, r *http.Request) {
          	//ParseFiles從"index.html"中解析模板。
          	//如果發(fā)生錯(cuò)誤,解析停止,返回的*Template為nil。
          	//當(dāng)解析多個(gè)文件時(shí),如果文件分布在不同目錄中,且具有相同名字的,將以最后一個(gè)文件為主。
          	files, _ := template.ParseFiles("index.html")
          	//聲明變量b,類型為字符串切片,有三個(gè)內(nèi)容
          	b := []string{"張無忌", "張三豐", "張翠山"}
          	//聲明變量b,類型為字符串切片,沒有內(nèi)容,是為了else示例
          	//b := []string{}
          	//Execute負(fù)責(zé)渲染模板,并將b寫入模板。
          	_ = files.Execute(w, b)
          
          	//w.Write([]byte("/index"))
          }
          func main() {
          	http.HandleFunc("/index", Index)
          	_ = http.ListenAndServe("", nil)
          }

          模板index.html的源碼及解析

          <!DOCTYPE html>
          <html lang="en">
          <head>
              <meta charset="UTF-8">
              <title>Title</title>
          </head>
          <body>
          <pre>
              {{range .}}
                  {{.}}
              {{else}}
                  None
              {{end}}
          </pre>
          </body>
          </html>

          測(cè)試http服務(wù),推薦使用httptest進(jìn)行單元測(cè)試

          package main
          
          import (
          	"io/ioutil"
          	"net/http"
          	"net/http/httptest"
          	"testing"
          )
          
          func TestIndex(t *testing.T) {
          	//初始化測(cè)試服務(wù)器
          	handler := http.HandlerFunc(Index)
          	app := httptest.NewServer(handler)
          	defer app.Close()
          	//測(cè)試代碼
          	//發(fā)送http.Get請(qǐng)求,獲取請(qǐng)求結(jié)果response
          	response, _ := http.Get(app.URL + "/index")
          	//關(guān)閉response.Body
          	defer response.Body.Close()
          	//讀取response.Body內(nèi)容,返回字節(jié)集內(nèi)容
          	bytes, _ := ioutil.ReadAll(response.Body)
          	//將返回的字節(jié)集內(nèi)容通過string()轉(zhuǎn)換成字符串,并顯示到日志當(dāng)中
          	t.Log(string(bytes))
          }

          執(zhí)行結(jié)果

          ain.go

          package main
          import (
              "fmt"
              "io"
              "io/ioutil"
              "net/http"
              "os"
          )
          //加載首頁的頁面
          func indexHandler(w http.ResponseWriter, r *http.Request) {
              data, err := ioutil.ReadFile("./static/index.html")
              if err != nil {
                  io.WriteString(w, "internel server error")
                  return
              }
              io.WriteString(w, string(data))
              //w.Write(data)
          }
          //接收表單參數(shù)
          func userHandler(w http.ResponseWriter, r *http.Request) {
              r.ParseForm() //解析參數(shù),默認(rèn)是不會(huì)解析的
              if r.Method == "GET" { //GET請(qǐng)求的方法
                  username := r.Form.Get("username") //必須使用雙引號(hào), 注意: 這里的Get 不是指只接收GET請(qǐng)求的參數(shù), 而是獲取參數(shù)
                  password := r.Form.Get("password")
                  //limit, _ := strconv.Atoi(r.Form.Get("limit")) //字符串轉(zhuǎn)整數(shù)的接收方法
                  //io.WriteString(w, username+":"+password)
                  w.Write([]byte(username + ":" + password))
              } else if r.Method == "POST" { //POST請(qǐng)求的方法
                  username := r.Form.Get("username") //必須使用雙引號(hào), 注意: POST請(qǐng)求, 也是用Get方法來接收
                  password := r.Form.Get("password")
                  //io.WriteString(w, username+":"+password)
                  w.Write([]byte(username + ":" + password))
              }
          }
          //文件上傳
          func uploadHandler(w http.ResponseWriter, r *http.Request) {
              r.ParseForm() //解析參數(shù),默認(rèn)是不會(huì)解析的
              if r.Method == "GET" { //GET請(qǐng)求的方法
                  data, err := ioutil.ReadFile("./static/upload.html")
                  if err != nil {
                      io.WriteString(w, "internel server error")
                      return
                  }
                  io.WriteString(w, string(data))
              } else if r.Method == "POST" { //POST請(qǐng)求的方法
                  // 接收文件流及存儲(chǔ)到本地目錄
                  file, head, err := r.FormFile("image") //接收文件域的方法
                  if err != nil {
                  fmt.Printf("Failed to get data, err:%s\n", err.Error())
                  return
              }
              defer file.Close()
              newFile, err := os.Create("C:/tmp/" + head.Filename) //創(chuàng)建文件
              if err != nil {
                  fmt.Printf("Failed to create file, err:%s\n", err.Error())
                  return
              }
              defer newFile.Close()
              FileSize, err := io.Copy(newFile, file) //拷貝文件
              if err != nil {
              fmt.Printf("Failed to save data into file, err:%s\n", err.Error())
              //http.Redirect(w, r, "/static/index.html", http.StatusFound) //重定向的方法
              return
              }
              io.WriteString(w, "上傳成功"+strconv.FormatInt(FileSize, 10)) //int64轉(zhuǎn)換成string
              }
          }
          func main() {
              // 靜態(tài)資源處理
              http.Handle("/static/",
              http.StripPrefix("/static/",
              http.FileServer(http.Dir("./static"))))
              // 動(dòng)態(tài)接口路由設(shè)置
              http.HandleFunc("/index", indexHandler)
              http.HandleFunc("/user", userHandler)
              http.HandleFunc("/upload", uploadHandler)
              // 監(jiān)聽端口
              fmt.Println("上傳服務(wù)正在啟動(dòng), 監(jiān)聽端口:8080...")
              err := http.ListenAndServe(":8080", nil)
              if err != nil {
              fmt.Printf("Failed to start server, err:%s", err.Error())
              }
          }

          static/upload.html

          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="utf-8">
          <title></title>
          </head>
          <body>
          <form action="/upload" method="POST" enctype="multipart/form-data">
          <input type="file" name="image" value="upload" />
          <input type="submit" value="上傳"/>
          </form>
          </body>
          </html>

          static/index.html

          <!DOCTYPE html>
          <html>
          <head>
          <meta charset="utf-8">
          <title></title>
          </head>
          <body>
          <p>這是首頁的內(nèi)容</p>
          </body>
          </html>
          D:\work\src>go run main.go
          上傳服務(wù)正在啟動(dòng), 監(jiān)聽端口:8080...

          訪問方式:

          http://localhost:8080/upload

          么是設(shè)計(jì)模式?


          設(shè)計(jì)模式是一套理論, 由軟件界先輩們總結(jié)出的一套可以反復(fù)使用的經(jīng)驗(yàn), 可以提高代碼可重用性, 增強(qiáng)系統(tǒng)可維護(hù)性, 以及巧妙解決一系列邏輯復(fù)雜的問題(運(yùn)用套路).

          1995 年,艾瑞克·伽馬(ErichGamma)、理査德·海爾姆(Richard Helm)、拉爾夫·約翰森(Ralph Johnson)、約翰·威利斯迪斯(John Vlissides)等 4 位作者合作出版了《設(shè)計(jì)模式:可復(fù)用面向?qū)ο筌浖幕A(chǔ)》(Design Patterns: Elements of Reusable Object-Oriented Software)一書,在本教程中收錄了 23 個(gè)設(shè)計(jì)模式,這是設(shè)計(jì)模式領(lǐng)域里程碑的事件,導(dǎo)致了軟件設(shè)計(jì)模式的突破。這 4 位作者在軟件開發(fā)領(lǐng)域里也以他們的“四人組”(Gang of Four,GoF)匿名著稱.

          項(xiàng)目簡(jiǎn)介


          Go 語言設(shè)計(jì)模式的實(shí)例代碼 + 代碼圖解

          項(xiàng)目地址:https://github.com/ssbandjl/golang-design-pattern

          創(chuàng)建型模式



          簡(jiǎn)單工廠模式(Simple Factory)

          工廠方法模式(Factory Method)

          抽象工廠模式(Abstract Factory)

          創(chuàng)建者模式(Builder)

          原型模式(Prototype)

          單例模式(Singleton)

          結(jié)構(gòu)型模式


          外觀模式(Facade)

          適配器模式(Adapter)

          代理模式(Proxy)

          組合模式(Composite)

          享元模式(Flyweight)

          裝飾模式(Decorator)


          橋接模式(Bridge)

          行為型模式

          • 中介者模式(Mediator)

          • 觀察者模式(Observer)

          • 命令模式(Command)

          • 迭代器模式(Iterator)

          • 模板方法模式(Template Method)

          • 策略模式(Strategy)

          • 狀態(tài)模式(State)

          • 備忘錄模式(Memento)

          • 解釋器模式(Interpreter)

          • 職責(zé)鏈模式(Chain of Responsibility)

          • 訪問者模式(Visitor)

          參考文檔


          廖雪峰:

          https://www.liaoxuefeng.com/wiki/1252599548343744/1281319417937953

          圖解設(shè)計(jì)模式: http://c.biancheng.net/view/1397.html

          golang-design-patttern: https://github.com/senghoo/golang-design-pattern



          END已結(jié)束

          歡迎大家留言, 訂閱, 交流哦!


          往期回顧


          [翻譯自官方]什么是RDB和AOF? 一文了解Redis持久化!

          Golang GinWeb框架9-編譯模板/自定義結(jié)構(gòu)體綁定/http2/操作Cookie/完結(jié)

          Golang GinWeb框架8-重定向/自定義中間件/認(rèn)證/HTTPS支持/優(yōu)雅重啟等

          Golang GinWeb框架7-靜態(tài)文件/模板渲染

          Golang GinWeb框架6-XML/JSON/YAML/ProtoBuf等渲染

          Golang GinWeb框架5-綁定請(qǐng)求字符串/URI/請(qǐng)求頭/復(fù)選框/表單類型

          Golang GinWeb框架4-請(qǐng)求參數(shù)綁定和驗(yàn)證

          Golang GinWeb框架3-自定義日志格式和輸出方式/啟禁日志顏色

          Golang GinWeb框架2-文件上傳/程序panic崩潰后自定義處理方式

          Golang GinWeb框架-快速入門/參數(shù)解析

          Golang與亞馬遜對(duì)象存儲(chǔ)服務(wù)AmazonS3快速入門

          Golang+Vue實(shí)現(xiàn)Websocket全雙工通信入門

          GolangWeb編程之控制器方法HandlerFunc與中間件Middleware

          Golang連接MySQL執(zhí)行查詢并解析-告別結(jié)構(gòu)體

          Golang的一種發(fā)布訂閱模式實(shí)現(xiàn)

          Golang 并發(fā)數(shù)據(jù)沖突檢測(cè)器(Data Race Detector)與并發(fā)安全

          Golang"驅(qū)動(dòng)"MongoDB-快速入門("快碼加鞭")


          點(diǎn)擊 "閱讀原文"獲得更好閱讀體驗(yàn)哦!點(diǎn)擊[在看], 推薦給其他小伙伴哦!


          主站蜘蛛池模板: 国产高清精品一区| 精品女同一区二区三区免费站 | 不卡无码人妻一区三区音频| 在线观看日韩一区| 无码精品人妻一区二区三区人妻斩| 无码视频一区二区三区| 无码人妻一区二区三区免费视频| 中文字幕久久亚洲一区| 亚洲国产精品自在线一区二区| 国产成人精品日本亚洲专一区| 国产亚洲福利精品一区二区 | 亚洲无线码在线一区观看| 久久免费区一区二区三波多野| 伊人色综合一区二区三区影院视频| 在线电影一区二区| 亚洲色精品aⅴ一区区三区| 无码人妻精品一区二区三区99性| 精品一区二区三区四区电影| 国产亚洲一区二区三区在线| 亚洲视频一区二区三区四区| 国产在线精品一区二区在线看 | 亚洲国产一区二区视频网站| 久久精品无码一区二区无码| 波多野结衣一区二区免费视频| 久久国产精品最新一区| 天天躁日日躁狠狠躁一区| 国产精品毛片一区二区三区| 动漫精品第一区二区三区| 精品人妻AV一区二区三区| 不卡无码人妻一区三区音频 | 一级特黄性色生活片一区二区| 日韩免费视频一区二区| 一区二区三区在线免费观看视频| 精品一区二区三区在线成人| 日韩有码一区二区| 无码日本电影一区二区网站 | 中文字幕一区二区三区久久网站| 日韩有码一区二区| 爆乳无码AV一区二区三区| 亚洲图片一区二区| 亚洲无线码在线一区观看|