Skip to content

Commit a81a46c

Browse files
committed
decorator
1 parent 7955b7b commit a81a46c

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
/.idea

Decorator/main.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package Decorator
2+
3+
import (
4+
"log"
5+
"math"
6+
"time"
7+
)
8+
9+
type piFunc func(int) float64
10+
11+
func Wraplogger(fun piFunc,logger *log.Logger) piFunc{
12+
return func(n int) float64{
13+
fn:=func(n int)(result float64){
14+
defer func(t time.Time){
15+
logger.Printf("took=%v,n=%v,result=%v",time.Since(t),n,result)
16+
}(time.Now())
17+
return fun(n)
18+
}
19+
return fn(n)
20+
}
21+
}
22+
23+
func Pi(n int) float64{
24+
ch := make(chan float64)
25+
for k:=0;k<=n;k++{
26+
go func(ch chan float64,k float64) {
27+
ch<-4*math.Pow(-1,k)/(2*k+1)
28+
}(ch,float64(k))
29+
}
30+
result :=0.0
31+
for k:=0;k<=n;k++{
32+
result +=<-ch
33+
}
34+
return result
35+
}

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/i-coder-robot/design-patterns-in-golang
2+
3+
go 1.13

main.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/i-coder-robot/design-patterns-in-golang/Decorator"
6+
"log"
7+
"os"
8+
)
9+
10+
func main(){
11+
fmt.Println(Decorator.Pi(1000))
12+
fmt.Println(Decorator.Pi(50000))
13+
14+
f:=Decorator.Wraplogger(Decorator.Pi,log.New(os.Stdout,"test",1))
15+
16+
f(100000)
17+
}

0 commit comments

Comments
 (0)