Go 程序员的进化史

Go

初级 Go 程序员

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
    }
}

学会如何在 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(Golang 创始人之一)

package fac

// Factorial returns n!.
func Factorial(n int) int {
    res := 1

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

    return res
}

改编自 “The Evolution of a Programmer“.

本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。

原文地址:https://github.com/SuperPaintman/the-evo...

译文地址:https://learnku.com/go/t/40983

本帖已被设为精华帖!
本文为协同翻译文章,如您发现瑕疵请点击「改进」按钮提交优化建议
讨论数量: 6

创始人告诉我们,大道至简,不要老是搞那些花里胡哨的

4年前 评论

创始人告诉我们,大道至简,不要老是搞那些花里胡哨的

4年前 评论

真丶少搞点花里胡哨 :stuck_out_tongue_closed_eyes:

4年前 评论

走着走着,又回归到最初了

4年前 评论

返璞归真?

4年前 评论

资深比初级多了注释,哈哈

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!