Created
April 22, 2020 02:04
-
-
Save foolishway/aa07c6eaf018bfe66e98edb32b342fa8 to your computer and use it in GitHub Desktop.
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" | |
type CB func(int, interface{}) | |
type Array []interface{} | |
func (arr *Array) forEach(callback CB) { | |
for index, item := range *arr { | |
callback(index, item) | |
} | |
} | |
func main() { | |
strArr := &Array{"hello", "word", "word", "word", "word", 123} | |
strArr.forEach(func(index int, item interface{}) { | |
if value, ok := item.(string); ok { | |
fmt.Printf("%d:%s\n", index, value) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment