Exercises & activities from the go workshop provided by Packt: https://courses.packtpub.com/courses/go
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

51 lines
867 B

package main
import "fmt"
const GlobalLimit = 100
const MaxCacheSize = 10 * GlobalLimit
const (
CacheKeyBook = "book_"
CacheKeyCD = "cd_"
)
var cache map[string]string
func cacheGet(key string) string {
return cache[key]
}
func cacheSet(key, val string) {
if len(cache)+1 >= MaxCacheSize {
return
}
cache[key] = val
}
func getBook(isbn string) string {
return cacheGet(CacheKeyBook + isbn)
}
func setBook(isbn, name string) {
cacheSet(CacheKeyBook+isbn, name)
}
func getCD(sku string) string {
return cacheGet(CacheKeyCD + sku)
}
func setCD(sku, title string) {
cacheSet(CacheKeyCD+sku, title)
}
func main() {
cache = make(map[string]string)
setBook("1234-5678", "Get ready to Go")
setCD("1234-5678", "Get ready to Go audio book")
fmt.Println("Book:", getBook("1234-5678"))
fmt.Println("CD :", getCD("1234-5678"))
}