mirror of
https://github.com/gopl-zh/gopl-zh.github.com.git
synced 2025-09-12 14:42:01 +00:00
good good study, day day up!
This commit is contained in:
40
vendor/gopl.io/ch9/memo1/memo.go
generated
vendored
Normal file
40
vendor/gopl.io/ch9/memo1/memo.go
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
// See page 272.
|
||||
|
||||
//!+
|
||||
|
||||
// Package memo provides a concurrency-unsafe
|
||||
// memoization of a function of type Func.
|
||||
package memo
|
||||
|
||||
// A Memo caches the results of calling a Func.
|
||||
type Memo struct {
|
||||
f Func
|
||||
cache map[string]result
|
||||
}
|
||||
|
||||
// Func is the type of the function to memoize.
|
||||
type Func func(key string) (interface{}, error)
|
||||
|
||||
type result struct {
|
||||
value interface{}
|
||||
err error
|
||||
}
|
||||
|
||||
func New(f Func) *Memo {
|
||||
return &Memo{f: f, cache: make(map[string]result)}
|
||||
}
|
||||
|
||||
// NOTE: not concurrency-safe!
|
||||
func (memo *Memo) Get(key string) (interface{}, error) {
|
||||
res, ok := memo.cache[key]
|
||||
if !ok {
|
||||
res.value, res.err = memo.f(key)
|
||||
memo.cache[key] = res
|
||||
}
|
||||
return res.value, res.err
|
||||
}
|
||||
|
||||
//!-
|
67
vendor/gopl.io/ch9/memo1/memo_test.go
generated
vendored
Normal file
67
vendor/gopl.io/ch9/memo1/memo_test.go
generated
vendored
Normal file
@@ -0,0 +1,67 @@
|
||||
// Copyright © 2016 Alan A. A. Donovan & Brian W. Kernighan.
|
||||
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
||||
|
||||
package memo_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gopl.io/ch9/memo1"
|
||||
"gopl.io/ch9/memotest"
|
||||
)
|
||||
|
||||
var httpGetBody = memotest.HTTPGetBody
|
||||
|
||||
func Test(t *testing.T) {
|
||||
m := memo.New(httpGetBody)
|
||||
memotest.Sequential(t, m)
|
||||
}
|
||||
|
||||
// NOTE: not concurrency-safe! Test fails.
|
||||
func TestConcurrent(t *testing.T) {
|
||||
m := memo.New(httpGetBody)
|
||||
memotest.Concurrent(t, m)
|
||||
}
|
||||
|
||||
/*
|
||||
//!+output
|
||||
$ go test -v gopl.io/ch9/memo1
|
||||
=== RUN Test
|
||||
https://golang.org, 175.026418ms, 7537 bytes
|
||||
https://godoc.org, 172.686825ms, 6878 bytes
|
||||
https://play.golang.org, 115.762377ms, 5767 bytes
|
||||
http://gopl.io, 749.887242ms, 2856 bytes
|
||||
|
||||
https://golang.org, 721ns, 7537 bytes
|
||||
https://godoc.org, 152ns, 6878 bytes
|
||||
https://play.golang.org, 205ns, 5767 bytes
|
||||
http://gopl.io, 326ns, 2856 bytes
|
||||
--- PASS: Test (1.21s)
|
||||
PASS
|
||||
ok gopl.io/ch9/memo1 1.257s
|
||||
//!-output
|
||||
*/
|
||||
|
||||
/*
|
||||
//!+race
|
||||
$ go test -run=TestConcurrent -race -v gopl.io/ch9/memo1
|
||||
=== RUN TestConcurrent
|
||||
...
|
||||
WARNING: DATA RACE
|
||||
Write by goroutine 36:
|
||||
runtime.mapassign1()
|
||||
~/go/src/runtime/hashmap.go:411 +0x0
|
||||
gopl.io/ch9/memo1.(*Memo).Get()
|
||||
~/gobook2/src/gopl.io/ch9/memo1/memo.go:32 +0x205
|
||||
...
|
||||
|
||||
Previous write by goroutine 35:
|
||||
runtime.mapassign1()
|
||||
~/go/src/runtime/hashmap.go:411 +0x0
|
||||
gopl.io/ch9/memo1.(*Memo).Get()
|
||||
~/gobook2/src/gopl.io/ch9/memo1/memo.go:32 +0x205
|
||||
...
|
||||
Found 1 data race(s)
|
||||
FAIL gopl.io/ch9/memo1 2.393s
|
||||
//!-race
|
||||
*/
|
Reference in New Issue
Block a user