<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                ## 創建存儲接口以實現數據可移植性 為了便于模擬,使用外部存儲接口時對操作進行抽象會很有幫助。這可以在更改存儲后端時提高可移植性。但如果需要在事務內執行多個操作,這樣做就比較尷尬,此時進行復合操作或允許通過上下文對象或其他函數參數傳遞會比較好。 本節將實現一個非常簡單的接口來處理MongoDB中的操作。我們會使用一個接口來持久化和檢索對象。 ### 實踐 1. 建立 storage.go: ``` package storage import "context" type Item struct { Name string Price int64 } // Storage是我們的存儲接口 將使用Mongo存儲實現它 type Storage interface { GetByName(context.Context, string) (*Item, error) Put(context.Context, *Item) error } ``` 2. 建立 mongoconfig.go: ``` package storage import mgo "gopkg.in/mgo.v2" // MongoStorage實現了storage 接口 type MongoStorage struct { *mgo.Session DB string Collection string } // NewMongoStorage 初始化MongoStorage func NewMongoStorage(connection, db, collection string) (*MongoStorage, error) { session, err := mgo.Dial("localhost") if err != nil { return nil, err } ms := MongoStorage{ Session: session, DB: db, Collection: collection, } return &ms, nil } ``` 3. 建立 mongointerface.go: ``` package storage import ( "context" "gopkg.in/mgo.v2/bson" ) // GetByName 查詢mongodb以獲取具有正確名稱的item func (m *MongoStorage) GetByName(ctx context.Context, name string) (*Item, error) { c := m.Session.DB(m.DB).C(m.Collection) var i Item if err := c.Find(bson.M{"name": name}).One(&i); err != nil { return nil, err } return &i, nil } // Put 添加一個item到mongo 中 func (m *MongoStorage) Put(ctx context.Context, i *Item) error { c := m.Session.DB(m.DB).C(m.Collection) return c.Insert(i) } ``` 4. 建立 exec.go: ``` package storage import ( "context" "fmt" ) // Exec 初始化存儲 storage然后使用存儲接口執行操作 func Exec() error { m, err := NewMongoStorage("localhost", "gocookbook", "items") if err != nil { return err } if err := PerformOperations(m); err != nil { return err } if err := m.Session.DB(m.DB).C(m.Collection).DropCollection(); err != nil { return err } return nil } // PerformOperations 演示創建并存入一個item然后對其進行查詢查詢 func PerformOperations(s Storage) error { ctx := context.Background() i := Item{Name: "candles", Price: 100} if err := s.Put(ctx, &i); err != nil { return err } candles, err := s.GetByName(ctx, "candles") if err != nil { return err } fmt.Printf("Result: %#v\n", candles) return nil } ``` 5. 建立 main.go: ``` package main import "github.com/agtorre/go-cookbook/chapter5/storage" func main() { if err := storage.Exec(); err != nil { panic(err) } } ``` 6. 這會輸出: ``` Result: &storage.Item{Name:"candles", Price:100} ``` ### 說明 在PerformOperation中我們可以看到,此函數將存儲接口作為參數。這意味著我們可以動態替換底層存儲,而無需修改此函數。如果想要將存儲連接到單獨的API以便使用和修改它將很簡單。 我們為這些接口添加了一個context,以增加額外的靈活性,并允許接口處理超時。將應用程序邏輯與底層存儲分離可提供多種好處,但是很難選擇正確的位置來處理邊界,這在不同的應用程序中會有很大差異。 * * * * 學識淺薄,錯誤在所難免。歡迎在群中就本書提出修改意見,以饗后來者,長風拜謝。 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>

                              哎呀哎呀视频在线观看