\u9996\u5148\u5b9a\u4e49\u51fd\u6570\u7c7b\u578b\uff0c\u63a5\u53d7House\u7c7b\u578b\u6307\u9488\uff1a<\/p>\n
type HouseOption func(*House)<\/pre>\n\u8fd9\u662f\u53ef\u9009\u51fd\u6570\u7684\u7b7e\u540d\uff0c\u4e0b\u9762\u5b9a\u4e49\u4e00\u4e9b\u53ef\u9009\u51fd\u6570\u7528\u4e8e\u4fee\u6539*House\u5b9e\u4f8b\uff1a<\/p>\n
func WithConcrete() HouseOption {\r\n return func(h *House) {\r\n h.Material = \"concrete\"\r\n }\r\n}\r\n \r\nfunc WithoutFireplace() HouseOption {\r\n return func(h *House) {\r\n h.HasFireplace = false\r\n }\r\n}<\/pre>\n\u4e0a\u9762\u6bcf\u4e2a\u51fd\u6570\u662f\u53ef\u9009\u6784\u9020\u51fd\u6570\uff0c\u8fd4\u56de\u53e6\u4e00\u4e2a\u51fd\u6570\uff0c\u5e26*House\u53c2\u6570\uff0c\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u6211\u4eec\u770b\u5230\u8fd4\u56de\u7684\u51fd\u6570\u4fee\u6539\u4e86*House\u5b9e\u4f8b\u7684\u5c5e\u6027\u3002\u8fd8\u53ef\u4ee5\u5b9e\u73b0\u5176\u4ed6\u53ef\u9009\u51fd\u6570\u7c7b\u578b\u7528\u4e8e\u4fee\u6539\u53c2\u6570\u5b9e\u4f8b\u5c5e\u6027\uff0c\u4e0b\u9762\u51fd\u6570\u8fd4\u56de\u4fee\u6539\u697c\u5c42\u7684\u53ef\u9009\u51fd\u6570\uff1a<\/p>\n
func WithFloors(floors int) HouseOption {\r\n return func(h *House) {\r\n h.Floors = floors\r\n }\r\n}<\/pre>\n\u589e\u5f3a\u6784\u9020\u51fd\u6570<\/strong><\/div>\n\u73b0\u5728\u7ec4\u5408\u53ef\u9009\u529f\u80fd\u51fd\u6570\u548c\u6784\u9020\u51fd\u6570\uff1a<\/p>\n
\/\/ NewHouse now takes a slice of option as the rest arguments\r\nfunc NewHouse(opts ...HouseOption) *House {\r\n const (\r\n defaultFloors = 2\r\n defaultHasFireplace = true\r\n defaultMaterial = \"wood\"\r\n )\r\n \r\n h := &House{\r\n Material: defaultMaterial,\r\n HasFireplace: defaultHasFireplace,\r\n Floors: defaultFloors,\r\n }\r\n \r\n \/\/ Loop through each option\r\n for _, opt := range opts {\r\n \/\/ Call the option giving the instantiated\r\n \/\/ *House as the argument\r\n opt(h)\r\n }\r\n \r\n \/\/ return the modified house instance\r\n return h\r\n}<\/pre>\n\u6784\u9020\u51fd\u6570\u63a5\u53d7\u4e00\u7ec4\u4efb\u610f\u6570\u91cf\u53ef\u9009\u529f\u80fd\u51fd\u6570\u4f5c\u4e3a\u53c2\u6570\uff0c\u9996\u6b21\u521d\u59cb\u5316House\u5c5e\u6027\u540e\uff0c\u4f9d\u6b64\u8fd0\u884c\u53ef\u9009\u529f\u80fd\u51fd\u6570\u4fee\u6539\u5c5e\u6027\u503c\u3002<\/p>\n
\u56de\u5230\u5f00\u59cb\u7684\u793a\u4f8b\uff0c\u73b0\u5728\u53ef\u4ee5\u5b9e\u73b0\u5e26\u53ef\u9009\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u8c03\u7528\uff1a<\/p>\n
h := NewHouse(\r\n WithConcrete(),\r\n WithoutFireplace(),\r\n WithFloors(3),\r\n)<\/pre>\n\u53ef\u9009\u6a21\u5f0f\u7684\u4f18\u52bf<\/p>\n
\u4e0a\u9762\u8ba8\u8bba\u4e86\u5982\u4f55\u5b9e\u73b0\u53ef\u9009\u6a21\u5f0f\uff0c\u8fd9\u91cc\u603b\u7ed3\u4e0b\u5176\u4f18\u52bf\u3002<\/p>\n
\u76f4\u89c2\u6e05\u6670<\/strong><\/span><\/div>\n\u76f8\u6bd4\u4e8e\u663e\u793a\u4fee\u6539\u5bf9\u8c61\u5c5e\u6027\uff1a<\/p>\n
h := NewHouse()\r\nh.Material = \"concrete\"<\/pre>\n\u53ef\u5229\u7528\u6784\u9020\u51fd\u6570\u76f4\u63a5\u5b9e\u73b0\uff1a<\/p>\n
h := NewHouse(WithConcrete())<\/pre>\n\u91c7\u7528\u8fd9\u79cd\u65b9\u5f0f\u66f4\u6e05\u6670\uff0c\u65e0\u9700\u6307\u5b9a\u5b57\u7b26\u4e32\u503c\uff0c\u907f\u514d\u6253\u5b57\u9519\u8bef\u5e76\u66b4\u9732*House\u5185\u90e8\u7ec6\u8282\u3002<\/p>\n
\u652f\u6301\u6269\u5c55<\/strong><\/span><\/div>\n\u53ef\u9009\u6a21\u5f0f\u652f\u6301\u6269\u5c55\uff0c\u603b\u662f\u652f\u6301\u4e0d\u540c\u53ef\u9009\u51fd\u6570\u53c2\u6570\u4f20\u5165\u6784\u9020\u51fd\u6570\u3002\u4e3e\u4f8b\uff0c\u65e2\u7136\u623f\u5c4b\u697c\u5c42\u53ef\u4ee5\u4e3a\u4efb\u4f55\u6574\u6570\uff0c\u6211\u4eec\u63d0\u4f9b\u5177\u4f53\u6570\u503c\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u6784\u9020\u51fd\u6570\uff1a<\/p>\n
h := NewHouse(WithFloors(4))<\/pre>\n\u53c2\u6570\u987a\u5e8f<\/strong><\/span><\/div>\n\u4f7f\u7528\u53ef\u9009\u6a21\u5f0f\u4e0e\u53c2\u6570\u987a\u5e8f\u65e0\u5173\uff0c\u76f8\u6bd4\u4e8e\u6b63\u5e38\u53c2\u6570\u6709\u5f88\u5927\u7684\u7075\u6d3b\u6027\uff1b\u800c\u4e14\uff0c\u53ef\u4ee5\u63d0\u4f9b\u4efb\u610f\u4e2a\u53ef\u9009\u53c2\u6570\uff0c\u76f8\u6bd4\u6b63\u5e38\u53c2\u6570\u5219\u5fc5\u987b\u63d0\u4f9b\u6240\u6709\u53c2\u6570\u3002<\/p>\n
\/\/ What `NewHouse` would look like if we used\r\n\/\/ regular function arguments\r\n\/\/ We would always need to provide all three\r\n\/\/ arguments no matter what\r\nh := NewHouse(\"concrete\", 5, true)<\/pre>\n\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8e\u5229\u7528Golang\u53ef\u9009\u53c2\u6570\u5b9e\u73b0\u53ef\u9009\u6a21\u5f0f\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"
\u672c\u6587\u8ba8\u8bbaGolang\u51fd\u6570\u53ef\u9009\u53c2\u6570\u53ca\u51fd\u6570\u7c7b\u578b\uff0c\u4ee5\u53ca\u5982\u4f55\u5229\u7528\u53ef\u9009\u51fd\u6570\u7c7b\u578b\u5b9e\u73b0\u53ef\u9009\u6a21\u5f0f\u3002\u540c\u65f6\u901a\u8fc7\u6784\u9020\u51fd\u6570\u4f5c\u4e3a\u793a\u4f8b\uff0c\u5b9e […]<\/p>\n","protected":false},"author":1482,"featured_media":258809,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-262971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=262971"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262971\/revisions"}],"predecessor-version":[{"id":262974,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262971\/revisions\/262974"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/258809"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=262971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=262971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=262971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}