用一个阶乘函数来演示一个 go 程序员的进化演变过程
Junior Go programmer 初级Go程序员
1
2
3
4
5
6
7
8
9
10
11
package fac
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Functional Go programmer 功能性Go程序员
1
2
3
4
5
6
7
8
9
package fac
func Factorial(n int) int {
if n == 0 {
return 1
} else {
return Factorial(n - 1) * n
}
}
Generic Go programmer 通用Go程序员
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package fac
func Factorial(n interface{}) interface{} {
v, valid := n.(int)
if !valid {
return 0
}
res := 1
for i := 1; i <= v; i++ {
res *= i
}
return res
}
Multithread optimized Go programmer 多线程优化的Go程序员
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package fac
import "sync"
func Factorial(n int) int {
var (
left, right = 1, 1
wg sync.WaitGroup
)
wg.Add(2)
pivot := n / 2
go func() {
for i := 1; i < pivot; i++ {
left *= i
}
wg.Done()
}()
go func() {
for i := pivot; i <= n; i++ {
right *= i
}
wg.Done()
}()
wg.Wait()
return left * right
}
Discovered Go patterns 发现探索型
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package fac
func Factorial(n int) <-chan int {
ch := make(chan int)
go func() {
prev := 1
for i := 1; i <= n; i++ {
v := prev * i
ch <- v
prev = v
}
close(ch)
}()
return ch
}
Fix Go weaknesses with mature solutions 使用成熟的解决方案修复Go的弱点
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package fac
/**
* @see https://en.wikipedia.org/wiki/Factorial
*/
type IFactorial interface {
CalculateFactorial() int
}
// FactorialImpl implements IFactorial.
var _ IFactorial = (*FactorialImpl)(nil)
/**
* Used to find factorial of the n.
*/
type FactorialImpl struct {
/**
* The n.
*/
n int
}
/**
* Constructor of the FactorialImpl.
*
* @param n the n.
*/
func NewFactorial(n int) *FactorialImpl {
return &FactorialImpl{
n: n,
}
}
/**
* Gets the n to use in factorial function.
*
* @return int.
*/
func (this *FactorialImpl) GetN() int {
return this.n
}
/**
* Sets the n to use in factorial function.
*
* @param n the n.
* @return void.
*/
func (this *FactorialImpl) SetN(n int) {
this.n = n
}
/**
* Returns factorial of the n.
*
* @todo remove "if" statement. Maybe we should use a factory or somthing?
*
* @return int.
*/
func (this *FactorialImpl) CalculateFactorial() int {
if this.n == 0 {
return 1
}
n := this.n
this.n = this.n - 1
return this.CalculateFactorial() * n
}
Senior Go programmer 高级Go程序员
1
2
3
4
5
6
7
8
9
10
11
12
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Rob Pike Go 语言之父
1
2
3
4
5
6
7
8
9
10
11
12
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
😄 可以断定: Rob Pike 是初级程序员 😂
关于 Rob Pike
Rob Pike 是Unix的先驱,是贝尔实验室最早和Ken Thompson以及 Dennis M. Ritche 开发Unix的猛人,UTF-8的设计人。他还在美国名嘴David Letterman 的晚间节目上露了一小脸,一脸憨厚地帮一胖子吹牛搞怪。让偶佩服不已的是,罗伯伯还是1980年奥运会射箭的银牌得主。他还是个颇为厉害的业余天文学家,设计的珈玛射线望远镜差点被NASA用在航天飞机上。Go 语言「元团队」里最为核心的人物,Rob Pike AT&T Bell Lab前Member of Technical Staff ,现在google研究操作系统。
本文网址: https://golangnote.com/topic/276.html 转摘请注明来源