<\/figure>\n\n\n\nBelow is the corresponding mapping UML diagram with the example given above<\/p>\n\n\n\n <\/figure>\n\n\n\n<\/p>\n\n\n\n
<\/span>Mapping:<\/strong><\/span><\/h2>\n\n\n\nThe below table represents the mapping from the UML diagram actors to actual implementation actors in “Example”<\/strong> below<\/p>\n\n\n\nProductFactory<\/td> gunFactory.go<\/td><\/tr> iProduct<\/td> iGun.go<\/td><\/tr> Product<\/td> gun.go<\/td><\/tr> Concrete iProduct 1<\/td> ak47go<\/td><\/tr> Concrete iProduct 1<\/td> maverick.go<\/td><\/tr> Client<\/td> main.go<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/p>\n\n\n\n
<\/span>Example: <\/strong><\/span><\/h2>\n\n\n\niGun.go<\/strong><\/p>\n\n\n\npackage main\n\ntype iGun interface {\n setName(name string)\n setPower(power int)\n getName() string\n getPower() int\n}<\/code><\/pre>\n\n\n\ngun.go<\/strong><\/p>\n\n\n\npackage main\n\ntype gun struct {\n name string\n power int\n}\n\nfunc (g *gun) setName(name string) {\n g.name = name\n}\n\nfunc (g *gun) getName() string {\n return g.name\n}\n\nfunc (g *gun) setPower(power int) {\n g.power = power\n}\n\nfunc (g *gun) getPower() int {\n return g.power\n}<\/code><\/pre>\n\n\n\nak47.go<\/strong><\/p>\n\n\n\npackage main\n\ntype ak47 struct {\n gun\n}\n\nfunc newAk47() iGun {\n return &ak47{\n gun: gun{\n name: \"AK47 gun\",\n power: 4,\n },\n }\n}<\/code><\/pre>\n\n\n\nmaverick.go<\/strong><\/p>\n\n\n\npackage main\n\ntype maverick struct {\n gun\n}\n\nfunc newMaverick() iGun {\n return &maverick{\n gun: gun{\n name: \"Maverick gun\",\n power: 5,\n },\n }\n}<\/code><\/pre>\n\n\n\ngunFactory.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc getGun(gunType string) (iGun, error) {\n if gunType == \"ak47\" {\n return newAk47(), nil\n }\n if gunType == \"maverick\" {\n return newMaverick(), nil\n }\n return nil, fmt.Errorf(\"Wrong gun type passed\")\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n ak47, _ := getGun(\"ak47\")\n maverick, _ := getGun(\"maverick\")\n printDetails(ak47)\n printDetails(maverick)\n}\n\nfunc printDetails(g iGun) {\n fmt.Printf(\"Gun: %s\", g.getName())\n fmt.Println()\n fmt.Printf(\"Power: %d\", g.getPower())\n fmt.Println()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nGun: AK47 gun\nPower: 4\nGun: Maverick gun\nPower: 5<\/code><\/pre>\n\n\n\n<\/p>\n\n\n\n
<\/span>Full Working Code:<\/strong><\/span><\/h2>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype iGun interface {\n setName(name string)\n setPower(power int)\n getName() string\n getPower() int\n}\n\ntype gun struct {\n name string\n power int\n}\n\nfunc (g *gun) setName(name string) {\n g.name = name\n}\n\nfunc (g *gun) getName() string {\n return g.name\n}\n\nfunc (g *gun) setPower(power int) {\n g.power = power\n}\n\nfunc (g *gun) getPower() int {\n return g.power\n}\n\ntype ak47 struct {\n gun\n}\n\nfunc newAk47() iGun {\n return &ak47{\n gun: gun{\n name: \"AK47 gun\",\n power: 4,\n },\n }\n}\n\ntype maverick struct {\n gun\n}\n\nfunc newMaverick() iGun {\n return &maverick{\n gun: gun{\n name: \"Maverick gun\",\n power: 5,\n },\n }\n}\n\nfunc getGun(gunType string) (iGun, error) {\n if gunType == \"ak47\" {\n return newAk47(), nil\n }\n if gunType == \"maverick\" {\n return newMaverick(), nil\n }\n return nil, fmt.Errorf(\"Wrong gun type passed\")\n}\n\nfunc main() {\n ak47, _ := getGun(\"ak47\")\n maverick, _ := getGun(\"maverick\")\n printDetails(ak47)\n printDetails(maverick)\n}\n\nfunc printDetails(g iGun) {\n fmt.Printf(\"Gun: %s\", g.getName())\n fmt.Println()\n fmt.Printf(\"Power: %d\", g.getPower())\n fmt.Println()\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nGun: AK47 gun\nPower: 4\nGun: Maverick gun\nPower: 5<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"Note: Interested in understanding how all other design patterns can be implemented in GO. Please see this full reference – All Design Patterns in Go (Golang) Table of Contents Introduction: UML Diagram:Mapping:Example: …<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","footnotes":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[1],"tags":[89,3,27],"class_list":["post-707","post","type-post","status-publish","format-standard","hentry","category-tech","tag-factory","tag-go","tag-pattern"],"yoast_head":"\n
Factory Design Pattern in Go (Golang) - Welcome To Golang By Example<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n