Go 程序员的演变,Rob Pike 亮了

栏目: IT技术 · 发布时间: 5年前

内容简介:通过一个阶乘函数的不同写法将 Go 程序员进行划分。来自:欢迎关注我的公众号:

通过一个阶乘函数的不同写法将 Go 程序员进行划分。

初级 Go 程序员

package fac

func Factorial(n int) int {
    res := 1

    for i := 1; i <= n; i++ {
        res *= i
    }

    return res
}

函数式 Go 程序员

package fac

func Factorial(n int) int {
    if n == 0 {
        return 1
    } else {
        return Factorial(n - 1) * n
    }
}

泛型 Go 程序员

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
}

多线程优化的 Go 程序员

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
}

发现型 Go 模式

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 缺陷

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
}

高级 Go 程序员

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
}

来自: https://github.com/SuperPaintman/the-evolution-of-a-go-programmer ,启发自《程序员的演变》 https://www.ariel.com.au/jokes/The_Evolution_of_a_Programmer.html

欢迎关注我的公众号:


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

失控的真相

失控的真相

[美] 迈克尔·帕特里克·林奇 / 赵亚男 / 中信出版社 / 2017-6 / 42.00元

编辑推荐 在信息泛滥的时代,知识变得无处不在。鼠标轻轻一点,我们就坐拥一座巨型图书馆。然而,我们并没有因此就离真相更近。相反,互联网的普及使人们早已习惯于凡事问搜索引擎,并形成了一种“搜索即相信”的认知模式。当社交网络把数字人类带入一个个彼此隔绝的线上群体中,我们清楚地看到,真相与谎言在互联网中交织,知识与观念混为一谈,情绪宣泄掩盖了事实分析。联网的世界让我们更容易看到彼此的观点,但同时也制......一起来看看 《失控的真相》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

html转js在线工具
html转js在线工具

html转js在线工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试