Last active
May 2, 2020 13:05
-
-
Save foolishway/f1a12de7ab4d672df7bfb485535f876d 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" | |
) | |
var serverMap = map[int32]string{1: "server1", 2: "server2", 3: "server3"} | |
func main() { | |
var id int32 = 1 | |
withId(id, func(s string) { | |
fmt.Printf("Get server %s by ID %d, then todo ...\n", s, id) | |
}) | |
id = 2 | |
withId(id, func(s string) { | |
fmt.Printf("Get server %s by ID %d, then todo ...\n", s, id) | |
}) | |
id = 4 | |
withId(id, func(s string) { | |
fmt.Printf("Get server %s by ID %d, then todo ...\n", s, id) | |
}) | |
} | |
func withId(id int32, fn func(string)) { | |
if server, ok := serverMap[id]; ok { | |
fn(server) | |
} else { | |
fmt.Println("Server not found.") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment