Skip to content

Instantly share code, notes, and snippets.

@DarkGhostHunter
Created October 27, 2020 03:09
Show Gist options
  • Save DarkGhostHunter/08dc3ecdba4a41a58d812cf66480537b to your computer and use it in GitHub Desktop.
Save DarkGhostHunter/08dc3ecdba4a41a58d812cf66480537b to your computer and use it in GitHub Desktop.
Simple iterator system
package main
import "fmt"
type KeyChain struct {
labels []string
keys []string
lock []string
}
func (kc KeyChain) Next(index int) bool {
// We only need the length of key labels.
return index < len(kc.labels)
}
func (kc KeyChain) Value(index int) (label, key, lock string) {
return kc.labels[index], kc.keys[index], kc.lock[index]
}
func main() {
var keychain = KeyChain{
labels: []string{"front-door", "backyard", "garage"},
keys: []string{"red", "blue", "green"},
lock: []string{"big", "simple", "wireless"},
}
for f := 0; keychain.Next(f); f++ {
label, key, lock := keychain.Value(f)
fmt.Println("Key Label: ", label, "\n Key: ", key, "\n Lock: ", lock)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment