Skip to content

Instantly share code, notes, and snippets.

@foolishway
Last active May 2, 2020 13:05
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/f1a12de7ab4d672df7bfb485535f876d to your computer and use it in GitHub Desktop.
Save foolishway/f1a12de7ab4d672df7bfb485535f876d to your computer and use it in GitHub Desktop.
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