溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

如何用Golang可選參數(shù)實現(xiàn)可選模式

發(fā)布時間:2023-01-30 09:13:06 來源:億速云 閱讀:115 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細介紹“如何用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ù),WithConcreteWithoutFireplace是傳入構(gòu)造函數(shù)的可選參數(shù),用于修改其返回值。下面會詳細WithConcreteWithoutFireplace可選功能函數(shù),有時它們比正常函數(shù)參數(shù)更有用。

定義構(gòu)造函數(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ù)

首先定義函數(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
	}
}

增強構(gòu)造函數(shù)

現(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),
)

可選模式的優(yōu)勢

上面討論了如何實現(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ù)順序

使用可選模式與參數(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è)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(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)容。

AI