--- url: "https://refactoring.guru/design-patterns/iterator/go/example" titulek: "Iterator in Go / Design Patterns" h1: "Iterator in Go" popis: "Iterator pattern in Go. Full code example in Go with detailed comments and explanation. Iterator is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details." jazyk: "en" kanonicka: "https://refactoring.guru/design-patterns/iterator/go/example" drobecky: ["Home", "Design Patterns", "Iterator", "Go"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-13 21:29:16" znaku: 1885 slov: 266 kodovani: "utf-8" sha256: "e48f7b245dfc1e5cce4fbdb098f060ae5484077e3417941e5cf46a2258d516c0" --- # **Iterator** in Go **Iterator** is a behavioral design pattern that allows sequential traversal through a complex data structure without exposing its internal details. Thanks to the Iterator, clients can go over elements of different collections in a similar fashion using a single iterator interface. [Learn more about Iterator](/design-patterns/iterator) ## Conceptual Example The main idea behind the Iterator pattern is to extract the iteration logic of a collection into a different object called iterator. This iterator provides a generic method of iterating over a collection independent of its type. #### **collection.go:** Collection ``` package main type Collection interface { createIterator() Iterator } ``` #### **userCollection.go:** Concrete collection ``` package main type UserCollection struct { users []*User } func (u *UserCollection) createIterator() Iterator { return &UserIterator{ users: u.users, } } ``` #### **iterator.go:** Iterator ``` package main type Iterator interface { hasNext() bool getNext() *User } ``` #### **userIterator.go:** Concrete iterator ``` package main type UserIterator struct { index int users []*User } func (u *UserIterator) hasNext() bool { if u.index < len(u.users) { return true } return false } func (u *UserIterator) getNext() *User { if u.hasNext() { user := u.users[u.index] u.index++ return user } return nil } ``` #### **user.go:** Client code ``` package main type User struct { name string age int } ``` #### **main.go:** Client code ``` package main import "fmt" func main() { user1 := &User{ name: "a", age: 30, } user2 := &User{ name: "b", age: 20, } userCollection := &UserCollection{ users: []*User{user1, user2}, } iterator := userCollection.createIterator() for iterator.hasNext() { user := iterator.getNext() fmt.Printf("User is %+v\n", user) } } ``` #### **output.txt:** Execution result ``` User is &{name:a age:30} User is &{name:b age:20} ``` ## **Iterator** in Other Languages