Created
October 27, 2020 03:09
-
-
Save DarkGhostHunter/08dc3ecdba4a41a58d812cf66480537b to your computer and use it in GitHub Desktop.
Simple iterator system
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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