当前位置:网站首页>The evolution history of Go programmers
The evolution history of Go programmers
2022-08-10 20:56:00 【User 9959267】
“写 n! several evolutions(退化)史
初级程序员
package fac
func Factorial(n int) int {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
return res
}
Programmers who learned recursion
package fac
func Factorial(n int) int {
if n == 0 {
return 1
} else {
return Factorial(n - 1) * n
}
}
Programmers who learned generics
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
}
just turnGo的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
}
边栏推荐
猜你喜欢

《分布式微服务电商》专题(一)-项目简介
[email protected] nanomimetic e"/>Water-soluble alloy quantum dot nanozymes|CuMoS nanozymes|porous silicon-based Pt(Au) nanozymes|[email protected] nanomimetic e

【图像分类】2017-MobileNetV1 CVPR

idea插件 协议 。。 公司申请软件用

【语义分割】2017-PSPNet CVPR

图扑智慧电力可视化大屏,赋能虚拟电厂精准减碳

leetcode 84.柱状图中最大的矩形 单调栈应用

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

知识图谱Knowledge Graph

.NET现代应用的产品设计 - DDD实践
随机推荐
单选点击可取消功能
「POJ 3666」Making the Grade 题解(两种做法)
这7个自动化办公模版 教你玩转表格数据自动化
C语言详解系列——关于调试那些事
cordova 安装错误 Command failed: powershell 解决方法
通用线程:POSIX 线程详解,第 2部分
铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
【SemiDrive源码分析】【MailBox核间通信】51 - DCF_IPCC_Property实现原理分析 及 代码实战
赎金信问题答记
Heme - gold nanoparticles (Heme - AuNP) composite nanometer enzyme | gold nanoparticles nuclear porous hollow carbon nanometer spherical shell (Au @ HCNs) nano enzyme
cordova installation error Command failed: powershell solution
C 语言 时间函数使用技巧(汇总)
苹果字体查找
金鱼哥RHCA回忆录:CL210OpenStack操作的故障排除--章节实验
壁仞推出全球最大算力芯片,号称以7nm超越英伟达4nm最新GPU
电信保温杯笔记——《统计学习方法(第二版)——李航》第17章 潜在语义分析
@Autowired annotation --required a single bean, but 2 were found causes and solutions
关于 NFT 版权保护的争议
【一致性hash】负载均衡器分发请求
svg+元素js实现在图片上描点成框,并获取相对图片的坐标位置