当前位置:网站首页>Go程序员进化史
Go程序员进化史
2022-08-10 20:28:00 【用户9959267】
“写 n! 的若干种进化(退化)史
初级程序员
package fac
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
学了递归的程序员
package fac
func Factorial(n int) int {
if n == 0 {
return 1
} else {
return Factorial(n - 1) * n
}
}
学了泛型的程序员
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
}
学了Goroutine的程序员
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
}
学了Channel的程序员
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
}
刚转Go的Java程序员
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
}
高级程序员
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Rob Pike
package fac
// Factorial returns n!.
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
边栏推荐
- Kyligence 通过 SOC 2 Type II 审计,以可信赖的企业级产品服务全球客户
- mysql踩坑----case when then用法
- 爬虫基本原理介绍、实现以及问题解决
- @Autowired annotation --required a single bean, but 2 were found causes and solutions
- OPPO Enco X2 迎来秋季产品升级 旗舰体验全面拉满
- 【一致性hash】负载均衡器分发请求
- 【SemiDrive源码分析】【MailBox核间通信】52 - DCF Notify 实现原理分析 及 代码实战
- C语言详解系列——关于调试那些事
- 转铁蛋白Tf功能化β-榄香烯-雷公藤红素/紫杉醇PLGA纳米粒/雷公藤甲素脂质体(化学试剂)
- 水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子
猜你喜欢

每日一R「03」Borrow 语义与引用

Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs

(10) Sequence and deserialization of image data

Apache DolphinScheduler 3.0.0 正式版发布!

【语义分割】2016-SegNet TPAMI

大小端的理解以及宏定义实现的理解
![[mysql] 深入分析MySQL版本控制MVCC规则](/img/16/e28641c355d941fda50a6e8b7911ee.png)
[mysql] 深入分析MySQL版本控制MVCC规则

Multifunctional Nanozyme Ag/PANI | Flexible Substrate Nano ZnO Enzyme | Rhodium Sheet Nanozyme | Ag-Rh Alloy Nanoparticle Nanozyme | Iridium Ruthenium Alloy/Iridium Oxide Biomimetic Nanozyme

【golang map】 深入了解map内部存储协议

铁蛋白-AHLL纳米颗粒|人表皮生长因子-铁蛋白重链亚基纳米粒子(EGF-5Cys-FTH1)|铁蛋白颗粒包载氯霉素Chloramphenicol-Ferritin
随机推荐
Demis Hassabis:AI 的强大,超乎我们的想象
QSslSocket has not been declared
一维数组动态和问题答记
这些mysql基础命令、基础知识还记得吗?(面试,学习,复习都可以)一万三千字总结
leetcode:45. 跳跃游戏II
双 TL431 级联振荡器
Tf ferritin particles contain cisplatin / oxaliplatin / doxorubicin / methotrexate MTX / paclitaxel PTX and other drugs
YOLOv3 SPP source analysis
报错:runtime error: reference binding to null pointer of type ‘std::vector<int, std::allocator<int>>‘
【语义分割】2016-SegNet TPAMI
【go】依赖注入
设备管理中数据聚类处理
spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
MySQL数据库的主从复制部署详解
铱钌合金/氧化铱仿生纳米酶|钯纳米酶|GMP-Pd纳米酶|金钯复合纳米酶|三元金属Pd-M-Ir纳米酶|中空金铂合金纳米笼核-多空二氧化硅壳纳米酶
opengrok搭建[通俗易懂]
A fullGC problem troubleshooting caused by groovy
ACM MM 2022 统一归一化:加速Transformer工业部署的归一化方法
YOLOv3 SPP源码分析
【语义分割】2015-UNet MICCAI