\u7ec4\u5408\u5b57\u9762\u91cf\u662f\u6700\u76f4\u63a5\u65b9\u5f0f\u521d\u59cb\u5316Go\u5bf9\u8c61\uff0c\u5047\u8bbe\u5b9a\u4e49\u4e86Book\u7c7b\u578b\uff0c\u4f7f\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n
type Book struct {\r\n title string\r\n pages int\r\n}\r\n\/\/ creating a new struct instance\r\nb := Book{}\r\n\/\/ creating a pointer to a struct instance\r\nbp := &Book{}\r\n\/\/ creating an empty value\r\nnothing := struct{}{}<\/pre>\n\u5f53\u7136\u8fd8\u53ef\u4ee5\u76f4\u63a5\u4e2a\u5c5e\u6027\u8d4b\u503c\uff1a<\/p>\n
b := Book{\r\n title: \"Julius Caesar\",\r\n pages: 322,\r\n}<\/pre>\n\u8fd9\u79cd\u65b9\u5f0f\u7684\u4f18\u52bf\u662f\u8bed\u6cd5\u76f4\u63a5\u3001\u7b80\u5355\u6613\u8bfb\u3002\u4f46\u4e0d\u80fd\u7ed9\u6bcf\u4e2a\u5c5e\u6027\u8bbe\u7f6e\u7f3a\u7701\u503c\u3002\u6240\u4ee5\u5f53\u7c7b\u578b\u5305\u62ec\u591a\u4e2a\u7f3a\u7701\u503c\u5b57\u6bb5\u65f6\uff0c\u9700\u8981\u91cd\u590d\u5199\u7f3a\u7701\u503c\u5b57\u6bb5\u8d4b\u503c\u8bed\u53e5\u3002\u4e3e\u4f8b\uff1a<\/p>\n
type Pizza struct {\r\n slices int\r\n toppings []string\r\n}\r\nsomePizza := Pizza{\r\n slices: 6,\r\n toppings: []string{\"pepperoni\"},\r\n}\r\notherPizza := Pizza{\r\n slices: 6,\r\n toppings: []string{\"onion\", \"pineapple\"},\r\n}<\/pre>\n\u4e0a\u9762\u793a\u4f8b\u6bcf\u6b21\u90fd\u8bbe\u7f6eslices\u5c5e\u6027\u4e3a6\uff0c\u53e6\u5916\uff0c\u5982\u679ctoppings\u5c5e\u6027\u53ef\u4ee5\u4e3a\u7a7a\uff0c\u5982\u679c\u6ca1\u6709\u521d\u59cb\u5316\u5219\u4e3anil\uff0c\u8fd9\u53ef\u80fd\u5bfc\u81f4\u9519\u8bef\u3002<\/p>\n
\u81ea\u5b9a\u4e49\u6784\u9020\u51fd\u6570<\/strong><\/div>\n\u5982\u679c\u5c5e\u6027\u9700\u8981\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u6216\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u81ea\u5b9a\u4e49\u6784\u9020\u51fd\u6570\u53ef\u80fd\u4f1a\u5f88\u6709\u7528\u3002\u4e0b\u9762\u901a\u8fc7NewPizza\u6784\u9020\u51fd\u6570\u5b9a\u4e49Pizza\u5b9e\u4f8b\uff1a<\/p>\n
func NewPizza(toppings []string) () {\r\n if toppings == nil {\r\n toppings = []string{}\r\n }\r\n return Pizza{\r\n slices: 6,\r\n toppings: toppings,\r\n }\r\n}<\/pre>\n\u901a\u8fc7\u4f7f\u7528\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b9e\u4f8b\u521b\u5efa\u8fc7\u7a0b\uff1a<\/p>\n
\u7ed9\u5b57\u6bb5\u8bbe\u7f6e\u7f3a\u7701\u503c\uff0c\u5f53\u7136\u8fd8\u53ef\u4ee5\u5229\u7528\u53ef\u9009\u53c2\u6570\u65b9\u5f0f\u7ed9\u4e0d\u540c\u5c5e\u6027\u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002
\n\u8fd8\u53ef\u4ee5\u6267\u884c\u5408\u7406\u6027\u68c0\u67e5\uff0c\u5982toppings\u662f\u5426\u4e3anil\u5e76\u521d\u59cb\u5316\u3002\u53ef\u4ee5\u5229\u7528make\u6216new\u6784\u9020\u4e00\u4e9b\u6570\u636e\u7c7b\u578b\u5e76\u66f4\u597d\u63a7\u5236\u5185\u5b58\u548c\u5bb9\u91cf\u3002<\/p>\n
\u4ece\u6784\u9020\u51fd\u6570\u8fd4\u56de\u9519\u8bef<\/strong><\/div>\n\u5f53\u6784\u9020\u5c5e\u6027\u65f6\uff0c\u53ef\u80fd\u4f9d\u8d56\u5176\u4ed6\u7cfb\u7edf\u6216\u5e93\u4f1a\u4ea7\u751f\u9519\u8bef\uff0c\u8fd9\u65f6\u6700\u597d\u8fd4\u56deerror\u3002<\/p>\n
func NewRemotePizza(url string) (Pizza, error) {\r\n \/\/ toppings are received from a remote URL, which may fail\r\n toppings, err := getToppings(url)\r\n if err != nil {\r\n \/\/ if an error occurs, return the wrapped error along with an empty\r\n \/\/ Pizza instance\r\n return Pizza{}, fmt.Errorf(\"could not construct new Pizza: %v\", err)\r\n }\r\n return Pizza{\r\n slices: 6,\r\n toppings: toppings,\r\n }, nil\r\n}<\/pre>\n\u8fd4\u56de\u9519\u8bef\u6709\u52a9\u4e8e\u5c06\u6545\u969c\u6761\u4ef6\u5c01\u88c5\u5728\u6784\u9020\u51fd\u6570\u672c\u8eab\u4e2d\u3002<\/p>\n
interface\u6784\u9020\u51fd\u6570<\/strong><\/div>\n\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u76f4\u63a5\u8fd4\u56deinterface\u7c7b\u578b\uff0c\u540c\u65f6\u5728\u5176\u4e2d\u521d\u59cb\u5316\u5177\u4f53\u7c7b\u578b\u3002\u5982\u679c\u6211\u4eec\u60f3\u5c06\u7ed3\u6784\u8bbe\u4e3a\u79c1\u6709\uff0c\u540c\u65f6\u5c06\u5176\u521d\u59cb\u5316\u8bbe\u4e3a\u516c\u5171\uff0c\u8fd9\u5c06\u5f88\u6709\u5e2e\u52a9\u3002<\/p>\n
\u8fd8\u662f\u7528Pizza\u7c7b\u578b\u4e3e\u4f8b\uff0c\u5982\u679c\u6709bakery\u63a5\u53e3\uff0c\u5224\u65adpizza\u662f\u5426\u53ef\u70d8\u70e4\u7c7b\u578b\u3002\u9996\u5148\u521b\u5efaBakeable\u63a5\u53e3\uff0c\u7136\u540e\u7ed9Pizza\u7c7b\u578b\u589e\u52a0isBaked\u5b57\u6bb5\uff1a<\/p>\n
\/\/ Pizza implements Bakeable\r\ntype Bakeable interface {\r\n Bake()\r\n}\r\ntype Pizza struct {\r\n slices int\r\n toppings []string\r\n isBaked bool\r\n}\r\nfunc (p Pizza) Bake() {\r\n p.isBaked = true\r\n}\r\n\/\/ this constructor will return a `Bakeable`\r\n\/\/ and not a `Pizza`\r\nfunc NewUnbakedPizza(toppings []string) Bakeable {\r\n return Pizza{\r\n slices: 6,\r\n toppings: toppings,\r\n }\r\n}<\/pre>\n\u6700\u4f73\u5b9e\u8df5<\/strong><\/div>\n\u8ba9\u6211\u4eec\u6765\u770b\u770bGo\u4e2d\u5173\u4e8e\u6784\u9020\u51fd\u6570\u547d\u540d\u548c\u7ec4\u7ec7\u7684\u4e00\u4e9b\u7ea6\u5b9a\uff1a<\/p>\n
\u57fa\u672c\u6784\u9020\u51fd\u6570<\/strong><\/div>\n\u5bf9\u4e8e\u7b80\u5355\u6784\u9020\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\uff08\u5982Abc\uff0c\u6216Xyz\u7c7b\u578b\uff09\uff0c\u5219\u51fd\u6570\u5206\u522b\u547d\u540d\u4e3aNewAbc\u548cNewXyz\u3002\u5bf9\u4e8ePizza\u5b9e\u4f8b\uff0c\u5219\u6784\u9020\u51fd\u6570\u547d\u540d\u4e3aNewPizza\u3002<\/p>\n
\u4e3b\u5305\u7c7b\u578b<\/strong><\/div>\n\u5982\u679c\u5728\u7ed9\u5b9a\u5305\u4e2d\uff0c\u521d\u59cb\u5316\u53d8\u91cf\u4e3a\u4e3b\u5305\u7c7b\u578b\uff0c\u53ef\u4ee5\u76f4\u63a5\u547d\u540d\u4e3aNew\uff08\u65e0\u9700\u524d\u7f00\uff09\u3002\u4e3e\u4f8b\uff0cPizza\u7ed3\u6784\u5b9a\u4e49\u5728pizza\u5305\u4e2d\uff0c\u6784\u9020\u51fd\u6570\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n
package pizza\r\ntype Pizza struct {\r\n \/\/ ...\r\n}\r\nfunc New(toppings []string) Pizza {\r\n \/\/ ...\r\n}<\/pre>\n\u5f53\u5728\u5176\u4ed6\u5305\u4e2d\u8c03\u7528\u51fd\u6570\u65f6\uff0c\u4ee3\u7801\u4e3a p := pizza.New() \u3002<\/p>\n
\u591a\u4e2a\u6784\u9020\u51fd\u6570<\/strong><\/div>\n\u6709\u65f6\u76f8\u540c\u7c7b\u578b\u53ef\u80fd\u6709\u591a\u4e2a\u6784\u9020\u51fd\u6570\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u4f7f\u7528NewXyz\u540d\u79f0\u7684\u53d8\u4f53\u6765\u63cf\u8ff0\u6bcf\u4e2a\u65b9\u6cd5\u3002\u4e3e\u4f8b\uff0c\u4e0b\u9762\u6709\u4e09\u4e2a\u65b9\u6cd5\u521b\u5efaPizza:<\/p>\n
NewPizza \u4e3a\u4e3b\u6784\u9020\u65b9\u6cd5.<\/li>\nNewRemotePizza \u57fa\u4e8e\u8fdc\u5904\u8d44\u6e90\u7684\u6784\u9020\u65b9\u6cd5.<\/li>\nNewUnbakedPizza \u8fd4\u56deBakeable\u63a5\u53e3\u7c7b\u578b\u7684\u6784\u9020\u65b9\u6cd5.<\/li>\n\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eGolang\u521b\u5efa\u6784\u9020\u51fd\u6570\u7684\u65b9\u6cd5\u8d85\u8be6\u7ec6\u8bb2\u89e3\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"
\u7ec4\u5408\u5b57\u9762\u91cf\u662f\u6700\u76f4\u63a5\u65b9\u5f0f\u521d\u59cb\u5316Go\u5bf9\u8c61\uff0c\u5047\u8bbe\u5b9a\u4e49\u4e86Book\u7c7b\u578b\uff0c\u4f7f\u7528\u5b57\u9762\u91cf\u521d\u59cb\u5316\u4ee3\u7801\u5982\u4e0b\uff1a type Book […]<\/p>\n","protected":false},"author":1482,"featured_media":222173,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-263028","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\/263028","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=263028"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/263028\/revisions"}],"predecessor-version":[{"id":263031,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/263028\/revisions\/263031"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/222173"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=263028"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=263028"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=263028"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}