<\/span><\/h1>\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\nCollection<\/td> collection.go<\/td><\/tr> Concrete Collection<\/td> userCollection.go<\/td><\/tr> Iterator<\/td> mac.go<\/td><\/tr> Concrete Iterator 1<\/td> userIterator.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><\/h1>\n\n\n\ncollection.go<\/strong><\/p>\n\n\n\npackage main\n\ntype collection interface {\n createIterator() iterator\n}<\/code><\/pre>\n\n\n\nuserCollection.go<\/strong><\/p>\n\n\n\npackage main\n\ntype userCollection struct {\n users []*user\n}\n\nfunc (u *userCollection) createIterator() iterator {\n return &userIterator{\n users: u.users,\n }\n}<\/code><\/pre>\n\n\n\niterator.go<\/strong><\/p>\n\n\n\npackage main\n\ntype iterator interface {\n hasNext() bool\n getNext() *user\n}<\/code><\/pre>\n\n\n\nuserIterator.go<\/strong><\/p>\n\n\n\npackage main\n\ntype userIterator struct {\n index int\n users []*user\n}\n\nfunc (u *userIterator) hasNext() bool {\n if u.index < len(u.users) {\n return true\n }\n return false\n}\n\nfunc (u *userIterator) getNext() *user {\n if u.hasNext() {\n user := u.users[u.index]\n u.index++\n return user\n }\n return nil\n}<\/code><\/pre>\n\n\n\nuser.go<\/strong><\/p>\n\n\n\npackage main\n\ntype user struct {\n name string\n age int\n}<\/code><\/pre>\n\n\n\nmain.go<\/strong><\/p>\n\n\n\npackage main\n\nimport \"fmt\"\n\nfunc main() {\n user1 := &user{\n name: \"a\",\n age: 30,\n }\n user2 := &user{\n name: \"b\",\n age: 20,\n }\n userCollection := &userCollection{\n users: []*user{user1, user2},\n }\n iterator := userCollection.createIterator()\n for iterator.hasNext() {\n user := iterator.getNext()\n fmt.Printf(\"User is %+v\\n\", user)\n }\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nUser is &{name:a age:30}\nUser is &{name:b age:20}<\/code><\/pre>\n\n\n\n<\/span>Full Working Code:<\/strong><\/span><\/h1>\n\n\n\npackage main\n\nimport \"fmt\"\n\ntype collection interface {\n createIterator() iterator\n}\n\ntype userCollection struct {\n users []*user\n}\n\nfunc (u *userCollection) createIterator() iterator {\n return &userIterator{\n users: u.users,\n }\n}\n\ntype iterator interface {\n hasNext() bool\n getNext() *user\n}\n\ntype userIterator struct {\n index int\n users []*user\n}\n\nfunc (u *userIterator) hasNext() bool {\n if u.index < len(u.users) {\n return true\n }\n return false\n}\n\nfunc (u *userIterator) getNext() *user {\n if u.hasNext() {\n user := u.users[u.index]\n u.index++\n return user\n }\n return nil\n}\n\ntype user struct {\n name string\n age int\n}\n\nfunc main() {\n user1 := &user{\n name: \"a\",\n age: 30,\n }\n user2 := &user{\n name: \"b\",\n age: 20,\n }\n userCollection := &userCollection{\n users: []*user{user1, user2},\n }\n iterator := userCollection.createIterator()\n for iterator.hasNext() {\n user := iterator.getNext()\n fmt.Printf(\"User is %+v\\n\", user)\n }\n}<\/code><\/pre>\n\n\n\nOutput:<\/strong><\/p>\n\n\n\nUser is &{name:a age:30}\nUser is &{name:b age:20}<\/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:MappingExampleFull Working…<\/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":[3,4,90],"class_list":["post-717","post","type-post","status-publish","format-standard","hentry","category-tech","tag-go","tag-golang","tag-iterator"],"yoast_head":"\n
Iterator Design Pattern in 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