Skip to content

Commit

Permalink
doc: imporve example about decorator with more params
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed Dec 11, 2023
1 parent 9a2ce36 commit a9f5508
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
12 changes: 8 additions & 4 deletions examples/decorator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,22 @@ func TestCacheFuncWithMoreParams(t *testing.T) {
}

// Cacheable Function
fnCached := gofnext.CacheFn1(fnWrap)
fnCachedInner := gofnext.CacheFn1(fnWrap)
fnCached := func(name string, age, gender int) int {
return fnCachedInner(Stu{name, age, gender})
}

// Execute the function multi times in parallel.
parallelCall(func() {
score := fnCached(Stu{"Alex", 20, 1})
score := fnCached("Alex", 20, 1)
if score != 10 {
t.Errorf("score should be 10, but get %d", score)
}
fnCached(Stu{"Jhon", 21, 0})
fnCached(Stu{"Alex", 20, 1})
fnCached("Jhon", 21, 0)
fnCached("Alex", 20, 1)
}, 10)

// Test count
if executeCount != 2 {
t.Errorf("executeCount should be 2, but get %d", executeCount)
}
Expand Down
14 changes: 9 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,23 @@ Refer to: [decorator example](https://github.com/ahuigo/gofnext/blob/main/exampl
}

// Cacheable Function
fnCached := gofnext.CacheFn1(fnWrap)
fnCachedInner := gofnext.CacheFn1(fnWrap)
fnCached := func(name string, age, gender int) int {
return fnCachedInner(Stu{name, age, gender})
}

// Execute the function multi times in parallel.
parallelCall(func() {
score := fnCached(Stu{"Alex", 20, 1})
score := fnCached("Alex", 20, 1)
if score != 10 {
t.Errorf("score should be 10, but get %d", score)
}
fnCached(Stu{"Jhon", 21, 0})
fnCached(Stu{"Alex", 20, 1})
fnCached("Jhon", 21, 0)
fnCached("Alex", 20, 1)
}, 10)

if executeCount != 2 {
// Test Count
if executeCount != 2 {
t.Errorf("executeCount should be 2, but get %d", executeCount)
}

Expand Down
14 changes: 9 additions & 5 deletions readme.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,23 @@ func main() {
}

// Cacheable Function
fnCached := gofnext.CacheFn1(fnWrap)
fnCachedInner := gofnext.CacheFn1(fnWrap)
fnCached := func(name string, age, gender int) int {
return fnCachedInner(Stu{name, age, gender})
}

// Execute the function multi times in parallel.
parallelCall(func() {
score := fnCached(Stu{"Alex", 20, 1})
score := fnCached("Alex", 20, 1)
if score != 10 {
t.Errorf("score should be 10, but get %d", score)
}
fnCached(Stu{"Jhon", 21, 0})
fnCached(Stu{"Alex", 20, 1})
fnCached("Jhon", 21, 0)
fnCached("Alex", 20, 1)
}, 10)

if executeCount != 2 {
// Test Count
if executeCount != 2 {
t.Errorf("executeCount should be 2, but get %d", executeCount)
}

Expand Down

0 comments on commit a9f5508

Please sign in to comment.