<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                ## 通過閉包實現集合操作 如果你一直在使用函數或動態編程語言,可能會覺得for循環和if語句會產生冗長的代碼。用于處理列表的函數構造(例如map和filter)通常很有用,并使代碼看起來更具可讀性。但是,在Go標準庫中很少支持類似的操作,并且在沒有泛型或非常復雜的映射和使用空接口的情況下很難使用。本節將提供使用Go閉包實現集合的一些基本示例。 ### 實踐 1. 創建collections.go : ``` package collections // WorkWith會實現集合接口 type WorkWith struct { Data string Version int } // Filter是一個過濾函數。 func Filter(ws []WorkWith, f func(w WorkWith) bool) []WorkWith { // 初始化返回值 result := make([]WorkWith, 0) for _, w := range ws { if f(w) { result = append(result, w) } } return result } // Map是一個映射函數。 func Map(ws []WorkWith, f func(w WorkWith) WorkWith) []WorkWith { // 返回值的長度應該與傳入切片長度一致 result := make([]WorkWith, len(ws)) for pos, w := range ws { newW := f(w) result[pos] = newW } return result } ``` 2. 創建functions.go : ``` package collections import "strings" // LowerCaseData 將傳入WorkWith的data字段變為小寫 func LowerCaseData(w WorkWith) WorkWith { w.Data = strings.ToLower(w.Data) return w } // IncrementVersion 將傳入WorkWith的Version加1 func IncrementVersion(w WorkWith) WorkWith { w.Version++ return w } // OldVersion 返回一個閉包,用于驗證版本是否大于指定的值 func OldVersion(v int) func(w WorkWith) bool { return func(w WorkWith) bool { return w.Version >= v } } ``` 3. 創建main.go: ``` package main import ( "fmt" "github.com/agtorre/go-cookbook/chapter3/collections" ) func main() { ws := []collections.WorkWith{ {"Example", 1}, {"Example 2", 2}, } fmt.Printf("Initial list: %#v\n", ws) ws = collections.Map(ws, collections.LowerCaseData) fmt.Printf("After LowerCaseData Map: %#v\n", ws) ws = collections.Map(ws, collections.IncrementVersion) fmt.Printf("After IncrementVersion Map: %#v\n", ws) ws = collections.Filter(ws, collections.OldVersion(3)) fmt.Printf("After OldVersion Filter: %#v\n", ws) } ``` 4. 這會輸出: ``` Initial list: []collections.WorkWith{collections.WorkWith{Data:"Example", Version:1}, collections.WorkWith{Data:"Example 2", Version:2}} After LowerCaseData Map: []collections.WorkWith{collections.WorkWith{Data:"example", Version:1}, collections.WorkWith{Data:"example 2", Version:2}} After IncrementVersion Map: []collections.WorkWith{collections.WorkWith{Data:"example", Version:2}, collections.WorkWith{Data:"example 2", Version:3}} After OldVersion Filter: []collections.WorkWith{collections.WorkWith{Data:"example 2", Version:3}} ``` ### 說明 Go中的閉包非常強大。雖然我們的集合函數不是通用的,但它們相對較小,并且很容易應用于WorkWith結構的各種函數。你可能會注意到我們在任何地方都沒有返回錯誤。而且我們返回了一個全新的切片。 如果需要將修改層應用于列表或列表結構,則此模式可以為節省大量的操作并使測試變得非常簡單。還可以將map和filter鏈接在一起,以獲得非常富有表現力的編碼風格。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 Golang中國(211938256) beego實戰(258969317) Go實踐(386056972)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看