Beginner Go Programmer

package fac

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

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

	return res
}
Copy the code

Learn the Go function

package fac

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

Learn Go generic programming

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
}
Copy the code

Learn to Go multithreaded optimization

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(a) {
		for i := 1; i < pivot; i++ {
			left *= i
		}

		wg.Done()
	}()

	go func(a) {
		for i := pivot; i <= n; i++ {
			right *= i
		}

		wg.Done()
	}()

	wg.Wait()

	return left * right
}
Copy the code

Learn the Go design pattern

package fac

func Factorial(n int) <-chan int {
	ch := make(chan int)

	go func(a) {
		prev := 1

		for i := 1; i <= n; i++ {
			v := prev * i

			ch <- v

			prev = v
		}

		close(ch)
	}()

	return ch
}
Copy the code

Learn a mature solution to Go’s shortcomings

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(a) 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(a) int {
	if this.n == 0 {
		return 1
	}

	n := this.n
	this.n = this.n - 1

	return this.CalculateFactorial() * n
}
Copy the code

Senior Go Programmer

package fac

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

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

	return res
}
Copy the code

Rob Pike, Co-founder of Golang

package fac

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

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

	return res
}
Copy the code

Original link: github.com/SuperPaintm…

Go on theme