Last active
August 26, 2015 08:18
-
-
Save 178inaba/0b96145fcf27168a909b to your computer and use it in GitHub Desktop.
get value from list. key increment and save. with go.
This file contains hidden or 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" | |
var gKey = 0 | |
func main() { | |
for i := 0; i < 100; i++ { | |
fmt.Println("--------------------") | |
fmt.Println("i:", i) | |
fmt.Println("gKey:", gKey) | |
fmt.Println("getVal():", getVal()) | |
} | |
fmt.Println("--------------------") | |
} | |
func getVal() string { | |
// get list | |
list := []string{"h", "e", "l", "l", "o", ",", "w", "o", "r", "l", "d", "!"} | |
// get key(use now) | |
key := gKey | |
// get value | |
v := list[key] | |
if key < len(list)-1 { | |
// key increment | |
key++ | |
} else { | |
key = 0 | |
} | |
// key save | |
gKey = key | |
return v | |
} |
This file contains hidden or 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
$ go run list.go | |
-------------------- | |
i: 0 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 1 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 2 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 3 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 4 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 5 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 6 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 7 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 8 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 9 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 10 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 11 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 12 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 13 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 14 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 15 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 16 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 17 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 18 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 19 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 20 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 21 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 22 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 23 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 24 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 25 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 26 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 27 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 28 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 29 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 30 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 31 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 32 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 33 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 34 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 35 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 36 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 37 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 38 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 39 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 40 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 41 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 42 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 43 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 44 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 45 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 46 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 47 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 48 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 49 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 50 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 51 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 52 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 53 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 54 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 55 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 56 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 57 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 58 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 59 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 60 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 61 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 62 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 63 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 64 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 65 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 66 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 67 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 68 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 69 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 70 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 71 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 72 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 73 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 74 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 75 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 76 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 77 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 78 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 79 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 80 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 81 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 82 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 83 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 84 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 85 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 86 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 87 | |
gKey: 3 | |
getVal(): l | |
-------------------- | |
i: 88 | |
gKey: 4 | |
getVal(): o | |
-------------------- | |
i: 89 | |
gKey: 5 | |
getVal(): , | |
-------------------- | |
i: 90 | |
gKey: 6 | |
getVal(): w | |
-------------------- | |
i: 91 | |
gKey: 7 | |
getVal(): o | |
-------------------- | |
i: 92 | |
gKey: 8 | |
getVal(): r | |
-------------------- | |
i: 93 | |
gKey: 9 | |
getVal(): l | |
-------------------- | |
i: 94 | |
gKey: 10 | |
getVal(): d | |
-------------------- | |
i: 95 | |
gKey: 11 | |
getVal(): ! | |
-------------------- | |
i: 96 | |
gKey: 0 | |
getVal(): h | |
-------------------- | |
i: 97 | |
gKey: 1 | |
getVal(): e | |
-------------------- | |
i: 98 | |
gKey: 2 | |
getVal(): l | |
-------------------- | |
i: 99 | |
gKey: 3 | |
getVal(): l | |
-------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment