Created
March 11, 2020 13:06
-
-
Save littlefuntik/78b1417a6cf67df28eb21fdcbcaddfa0 to your computer and use it in GitHub Desktop.
Go iterations
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" | |
func main() { | |
var funcs1 []func() | |
var funcs2 []func() | |
for _, v := range []string{"create", "update", "delete"} { | |
fmt.Println(v, &v) | |
funcs1 = append(funcs1, func () { fmt.Println("func1 "+ v) }) | |
vCopy := v | |
funcs2 = append(funcs2, func () { fmt.Println("func2 "+ vCopy) }) | |
} | |
for _, f := range funcs1 { f() } | |
for _, f := range funcs2 { f() } | |
} | |
/** | |
Interesting output: | |
create 0xc00007c1e0 | |
update 0xc00007c1e0 | |
delete 0xc00007c1e0 | |
func1 delete | |
func1 delete | |
func1 delete | |
func2 create | |
func2 update | |
func2 delete | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment