Skip to content

Instantly share code, notes, and snippets.

@betandr
Last active February 5, 2019 17: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 betandr/5071c07f48a72e86ce01012837a239c2 to your computer and use it in GitHub Desktop.
Save betandr/5071c07f48a72e86ce01012837a239c2 to your computer and use it in GitHub Desktop.
Unnamed Struct Type Method
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