日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

Golang流水線設計模式實踐

來源: 責編: 時間:2024-01-08 09:16:19 204觀看
導讀到目前為止,我已經將Golang整合到項目中有一段時間了,Golang是一種非常強大的語言,我渴望在其生態系統中進一步磨練技能。基于項目的特定需求,我需要實現流水線模式(Pipeline Pattern),數據需要通過多個過濾器,以順序的方式

到目前為止,我已經將Golang整合到項目中有一段時間了,Golang是一種非常強大的語言,我渴望在其生態系統中進一步磨練技能。sPq28資訊網——每日最新資訊28at.com

sPq28資訊網——每日最新資訊28at.com

基于項目的特定需求,我需要實現流水線模式(Pipeline Pattern),數據需要通過多個過濾器,以順序的方式進行處理。讓我解釋一下:sPq28資訊網——每日最新資訊28at.com

假設我們有一個很長的字符串。第一步是根據特定標準對其進行解析。接下來,需要對解析后的數據進行一些調整。隨后,將解析后的數據保存到數據庫中。正如你所見,有多個任務需要處理,而每個任務都是相互關聯的。sPq28資訊網——每日最新資訊28at.com

sPq28資訊網——每日最新資訊28at.com

基礎流水線sPq28資訊網——每日最新資訊28at.com

在軟件開發中,可以用流水線設計模式(pipeline design pattern) 來管理這種場景,該模式是為順序處理對象修改而設計的。想象有一條裝配流水線,每個工位都是一段"管道(pipe)",當某個物體通過整個流水線后,就發生了變化。從本質上講,流水線負責將值通過一系列可調用的"管道(pipe)"(無論是中間件、過濾器還是處理器)進行順序傳遞。在將該值傳遞給序列中的后續管道之前,每個管道段都有可能改變該值。該模式在諸如請求處理、數據處理或轉換等場景中特別有用,提供了一種干凈、可維護和可測試的方法。sPq28資訊網——每日最新資訊28at.com

作為解決方案,我準備了一個簡單的Golang包,可以在處理流程中使用流水線模式,它建立在責任鏈(chain of responsibility, CoR) 設計模式之上,可以將其安裝到項目中并使用:sPq28資訊網——每日最新資訊28at.com

go get github.com/izniburak/pipeline-go

包安裝之后,可以做一個簡單演示。首先需要一些新的結構體,這些結構體具有從PipeInterface實現的Handle方法。因為pipeline包需要多個流水線,所以我們用Handle方法來運行每個流水線:sPq28資訊網——每日最新資訊28at.com

package mainimport ( "strings" "github.com/izniburak/pipeline-go")type UpperCasePipe struct{}func (u *UpperCasePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {  // get value  text := value.(string)  capitalized := strings.ToUpper(text)  return next(capitalized)}type TrimSpacePipe struct{}func (t *TrimSpacePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {  // get value  text := value.(string)  trimmed := strings.Trim(text, " ")  return next(trimmed)}

然后可以開始使用流水線:sPq28資訊網——每日最新資訊28at.com

package mainimport ( "fmt" "strings" "github.com/izniburak/pipeline-go")type UpperCasePipe struct{}func (u *UpperCasePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {  // get value  text := value.(string)  capitalized := strings.ToUpper(text)  return next(capitalized)}type TrimSpacePipe struct{}func (t *TrimSpacePipe) Handle(value pipeline.PipeValue, next pipeline.PipeNext) pipeline.PipeValue {  // get value  text := value.(string)  trimmed := strings.Trim(text, " ")  return next(trimmed)}func main() {  text := "   buki.dev   "  pipes := []pipeline.PipeInterface{    new(UpperCasePipe),    new(TrimSpacePipe),  }  result := pipeline.Send(text).Through(pipes).ThenReturn()  fmt.Println(result) // BUKI.DEV}

如你所見,我們使用了兩個不同的管道,分別是UpperCasePipe和TrimSpacePipe。輸入是 buki.dev ,兩邊都有空格,輸出是BUKI.DEV。sPq28資訊網——每日最新資訊28at.com

就是這樣。流水線非常有用,是吧?sPq28資訊網——每日最新資訊28at.com

可以在GitHub上查看pipeline包[2]。sPq28資訊網——每日最新資訊28at.com

參考資料:sPq28資訊網——每日最新資訊28at.com

  • [1]Using a Pipeline Pattern in Golang: https://tech.jotform.com/pipeline-go-pipeline-pattern-in-golang-5a51e5d811a
  • [2]pipeline-go: https://github.com/izniburak/pipeline-go

本文鏈接:http://www.www897cc.com/showinfo-26-57888-0.htmlGolang流水線設計模式實踐

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 什么是Helm?它是如何提升云原生應用私有化部署效率的

下一篇: 十個Java編程中記錄日志的小技巧

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 新化县| 宿州市| 庄浪县| 尚义县| 蒙城县| 万山特区| 青州市| 阿拉尔市| 山东| 临汾市| 寻甸| 八宿县| 来宾市| 宣化县| 泰安市| 奎屯市| 公安县| 普安县| 河东区| 宝山区| 建始县| 榆中县| 涿州市| 徐州市| 罗田县| 彭州市| 伽师县| 大埔区| 体育| 镇巴县| 丹凤县| 宁国市| 进贤县| 龙游县| 淄博市| 富裕县| 香港| 蓬溪县| 永安市| 大邑县| 拜泉县|