您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“如何用Golang可選參數(shù)實現(xiàn)可選模式”,內(nèi)容詳細,步驟清晰,細節(jié)處理妥當,希望這篇“如何用Golang可選參數(shù)實現(xiàn)可選模式”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
可選參數(shù)給函數(shù)傳遞額外參數(shù)擴展或修改其行為,下面示例利用可選功能創(chuàng)建House類型:
h := NewHouse( WithConcrete(), WithoutFireplace(), )
NewHouse
是構(gòu)造函數(shù),WithConcrete
和WithoutFireplace
是傳入構(gòu)造函數(shù)的可選參數(shù),用于修改其返回值。下面會詳細WithConcrete
和WithoutFireplace
可選功能函數(shù),有時它們比正常函數(shù)參數(shù)更有用。
首先定義要利用可選功能的結(jié)構(gòu)體:
type House struct { Material string HasFireplace bool Floors int } // `NewHouse` is a constructor function for `*House` func NewHouse() *House { const ( defaultFloors = 2 defaultHasFireplace = true defaultMaterial = "wood" ) h := &House{ Material: defaultMaterial, HasFireplace: defaultHasFireplace, Floors: defaultFloors, } return h }
House
可能采用不同材料,有多層,并可能包括壁爐。NewHouse
構(gòu)造函數(shù)返回House
指針,所有屬性包括缺省值。正常情況下,首先構(gòu)造House
,然后根據(jù)不同需求修改屬性值。使用函數(shù)可選參數(shù),可以給構(gòu)造函數(shù)傳入一組修改器函數(shù)。
首先定義函數(shù)類型,接受House
類型指針:
type HouseOption func(*House)
這是可選函數(shù)的簽名,下面定義一些可選函數(shù)用于修改*House
實例:
func WithConcrete() HouseOption { return func(h *House) { h.Material = "concrete" } } func WithoutFireplace() HouseOption { return func(h *House) { h.HasFireplace = false } }
上面每個函數(shù)是可選構(gòu)造函數(shù),返回另一個函數(shù),帶*House
參數(shù),沒有返回值。我們看到返回的函數(shù)修改了*House
實例的屬性。還可以實現(xiàn)其他可選函數(shù)類型用于修改參數(shù)實例屬性,下面函數(shù)返回修改樓層的可選函數(shù):
func WithFloors(floors int) HouseOption { return func(h *House) { h.Floors = floors } }
現(xiàn)在組合可選功能函數(shù)和構(gòu)造函數(shù):
// NewHouse now takes a slice of option as the rest arguments func NewHouse(opts ...HouseOption) *House { const ( defaultFloors = 2 defaultHasFireplace = true defaultMaterial = "wood" ) h := &House{ Material: defaultMaterial, HasFireplace: defaultHasFireplace, Floors: defaultFloors, } // Loop through each option for _, opt := range opts { // Call the option giving the instantiated // *House as the argument opt(h) } // return the modified house instance return h }
構(gòu)造函數(shù)接受一組任意數(shù)量可選功能函數(shù)作為參數(shù),首次初始化House屬性后,依此運行可選功能函數(shù)修改屬性值。
回到開始的示例,現(xiàn)在可以實現(xiàn)帶可選參數(shù)的構(gòu)造函數(shù)調(diào)用:
h := NewHouse( WithConcrete(), WithoutFireplace(), WithFloors(3), )
上面討論了如何實現(xiàn)可選模式,這里總結(jié)下其優(yōu)勢。
相比于顯示修改對象屬性:
h := NewHouse() h.Material = "concrete"
可利用構(gòu)造函數(shù)直接實現(xiàn):
h := NewHouse(WithConcrete())
采用這種方式更清晰,無需指定字符串值,避免打字錯誤并暴露*House內(nèi)部細節(jié)。
可選模式支持擴展,總是支持不同可選函數(shù)參數(shù)傳入構(gòu)造函數(shù)。舉例,既然房屋樓層可以為任何整數(shù),我們提供具體數(shù)值作為參數(shù)傳入構(gòu)造函數(shù):
h := NewHouse(WithFloors(4))
使用可選模式與參數(shù)順序無關(guān),相比于正常參數(shù)有很大的靈活性;而且,可以提供任意個可選參數(shù),相比正常參數(shù)則必須提供所有參數(shù)。
// What `NewHouse` would look like if we used // regular function arguments // We would always need to provide all three // arguments no matter what h := NewHouse("concrete", 5, true)
讀到這里,這篇“如何用Golang可選參數(shù)實現(xiàn)可選模式”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。