Skip to content

Instantly share code, notes, and snippets.

@foolishway
Created April 22, 2020 02:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foolishway/aa07c6eaf018bfe66e98edb32b342fa8 to your computer and use it in GitHub Desktop.
Save foolishway/aa07c6eaf018bfe66e98edb32b342fa8 to your computer and use it in GitHub Desktop.
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