Last active
February 5, 2019 17:04
-
-
Save betandr/5071c07f48a72e86ce01012837a239c2 to your computer and use it in GitHub Desktop.
Unnamed Struct Type Method
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" | |
"sync" | |
) | |
var cache = struct { | |
sync.Mutex // Mutex is embedded so its methods are promoted to the unnamed struct type | |
mapping map[string]string | |
}{ | |
mapping: make(map[string]string), | |
} | |
func Lookup(key string) (val string) { | |
cache.Lock() | |
val = cache.mapping[key] | |
cache.Unlock() | |
return | |
} | |
func main() { | |
fmt.Println(Lookup("foo")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment