<\/span><\/h2>\n\n\n\nThe below table represents the mapping from the UML diagram actors to actual implementation actors in code.<\/p>\n\n\n\nprototype interface<\/td> inode.go<\/td><\/tr> Concrete Prototype 1<\/td> file.go<\/td><\/tr> Concrete Prototype 2<\/td> folder.go<\/td><\/tr> client<\/td> main.go<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<\/span>Practical Example<\/strong>:<\/span><\/h2>\n\n\n\nIn the context of golang let’s try to understand it with an example of os file system. The os file system has files and folders and folders itself contain files and folders. Each file<\/strong> and folder<\/strong> can be represented by an inode<\/strong> interface. inode<\/strong> interface also has the clone<\/strong>() function.<\/p>\n\n\n\ninode.go<\/strong><\/p>\n\n\n\npackage main\n\ntype inode interface {\n print(string)\n clone() inode\n}<\/code><\/pre>\n\n\n\nfile<\/strong> struct is represented as <\/p>\n\n\n\nfile.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype file struct {\n\tname string\n}\n\nfunc (f *file) print(indentation string) {\n\tfmt.Println(indentation + f.name)\n}\n\nfunc (f *file) clone() inode {\n\treturn &file{name: f.name + \"_clone\"}\n}\n<\/code><\/pre>\n\n\n\nfolder<\/strong> struct is represented as<\/p>\n\n\n\nfolder.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype folder struct {\n\tchildrens []inode\n\tname string\n}\n\nfunc (f *folder) print(indentation string) {\n\tfmt.Println(indentation + f.name)\n\tfor _, i := range f.childrens {\n\t\ti.print(indentation + indentation)\n\t}\n}\n\nfunc (f *folder) clone() inode {\n\tcloneFolder := &folder{name: f.name + \"_clone\"}\n\tvar tempChildrens []inode\n\tfor _, i := range f.childrens {\n\t\tcopy := i.clone()\n\t\ttempChildrens = append(tempChildrens, copy)\n\t}\n\tcloneFolder.childrens = tempChildrens\n\treturn cloneFolder\n}\n<\/code><\/pre>\n\n\n\nSince both file<\/strong> and folder<\/strong> struct implements the print<\/strong> and clone<\/strong> functions, hence they are of type inode<\/strong>. Also, notice the clone<\/strong> function in both file and folder. The clone<\/strong> function in both of them returns a copy of the respective file or folder. While cloning we append the keyword “_clone” for the name field. Let’s write the main function to test things out.<\/p>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n file1 := &file{name: \"File1\"}\n file2 := &file{name: \"File2\"}\n file3 := &file{name: \"File3\"}\n folder1 := &folder{\n childrens: []inode{file1},\n name: \"Folder1\",\n }\n folder2 := &folder{\n childrens: []inode{folder1, file2, file3},\n name: \"Folder2\",\n }\n fmt.Println(\"\\nPrinting hierarchy for Folder2\")\n folder2.print(\" \")\n cloneFolder := folder2.clone()\n fmt.Println(\"\\nPrinting hierarchy for clone Folder\")\n cloneFolder.print(\" \")\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nPrinting hierarchy for Folder2\n Folder2\n Folder1\n File1\n File2\n File3\n\nPrinting hierarchy for clone Folder\n Folder2_clone\n Folder1_clone\n File1_clone\n File2_clone\n File3_clone<\/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 Definition:When to UseUML…<\/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":[25,3,4,44],"class_list":["post-305","post","type-post","status-publish","format-standard","hentry","category-tech","tag-design-pattern","tag-go","tag-golang","tag-prototype"],"yoast_head":"\n
Prototype 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