GolangNote

Golang笔记

一个Golang程序员的代码进化演变

Permalink

用一个阶乘函数来演示一个 go 程序员的进化演变过程

Junior Go programmer 初级Go程序员

Go: 初级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程序员

Go: 功能性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程序员

Go: 通用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程序员

Go: 多线程优化的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 发现探索型

Go: 发现探索型
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的弱点

Go: 使用成熟的解决方案修复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程序员

Go: 高级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 语言之父

Go: 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 转摘请注明来源

Related articles

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

Golang 泛型性能初识

编程时,我们通常需要编写“泛型”函数,其中确切的数据类型并不重要。例如,您可能想编写一个简单的函数来对数字进行求和。Go 直到最近才有这个概念,但最近才添加了它(从1.18版开始)。...

Golang实现简单的Socks5代理

Socks5 代理较 `http/https` 代理有较好的性能,下面是借鉴某个著名开源软件的 local 实现的简单代理。...

Write a Comment to "一个Golang程序员的代码进化演变"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms