Go 程序员的演变,最后的 “Rob Pike” 这个梗看懂了吗?

简介: Go 程序员的演变,最后的 “Rob Pike” 这个梗看懂了吗?

通过一个阶乘函数的不同写法将 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

相关文章
|
2月前
|
存储 安全 Java
Java 程序员极速上手 go
Java 程序员极速上手 go
|
9月前
|
存储 缓存 安全
90%的Go语言程序员map遍历方式都是错的
90%的Go语言程序员map遍历方式都是错的
58 0
|
程序员 Go
我的Go+语言初体验——Demo游戏体验篇(直男程序员的真实体验)
我的Go+语言初体验——Demo游戏体验篇(直男程序员的真实体验)
我的Go+语言初体验——Demo游戏体验篇(直男程序员的真实体验)
|
IDE 程序员 测试技术
我的Go+语言初体验——语法验证/性能测试篇(直男程序员的真实体验1)
我的Go+语言初体验——语法验证/性能测试篇(直男程序员的真实体验)
我的Go+语言初体验——语法验证/性能测试篇(直男程序员的真实体验1)
|
算法 JavaScript 程序员
我的Go+语言初体验——环境搭建篇(直男程序员的真实体验)
我的Go+语言初体验——环境搭建篇(直男程序员的真实体验)
我的Go+语言初体验——环境搭建篇(直男程序员的真实体验)
|
程序员 测试技术 Go
我的Go+语言初体验——语法验证/性能测试篇(直男程序员的真实体验2)
我的Go+语言初体验——语法验证/性能测试篇(直男程序员的真实体验)
|
程序员 Go
听说,99% 的 Go 程序员都被 defer 坑过
听说,99% 的 Go 程序员都被 defer 坑过
145 0
|
Web App开发 测试技术 程序员
GO编程程序员修炼秘籍:十本经典书单
随着BAT、今日头条、京东、抖音等大型互联网公司对Go语言的大范围应用,带动更多互联网企业采取技术跟随战略,Go语言发展前景一片大好。
3147 0