Categories

A variadic function accept varying numbers of arguments.

To declare a variadic function, the type of of the final parameter is preceded by an ellipsis, “…”

``func sum(vals ...int) int {}``

…int ellipsis defined a function can be called with any number of arguments of this type

vals is []int slice, can be access this slice using index or for..range

Categories

## Methods with Struct

``````type Point struct {
x  float64
y  float64
}``````

The Point struct type has two fields, x and y of float64

``````func (p Point) DistToOrgin()  {
t := math.Pow(p.x, 2) + math.Pow(p.y, 2)​
return math.Sqrt(t)​
}``````

A method is defined with use of the keyword “func” just like a definition of the function. Just after func keyword, the defined fields of struct used in the body of code of the function as a receiver. It takes a variable of struct type “p Point” as a receiver (input) to the method name DistToOrgin().

Categories

## Function with a function return.

func foo() func() int { }

A function name foo() without parameters/arguments return anonymous function without parameter/arguments return an int type. Empty body

``````1 func foo()  func() int {
2      return func() int {
3            return 1
4      }
5
6 }``````

In the line 2, the return for calling func foo() and follow up with the code of anonmoys function func() with return int type signature. In this body of anonymous func() on line 3 the code return 1.

So when a func foo() called, the body code of func foo() executed instead of just return the results. The results of func foo() pass into anonymous function to be process and then return the results.

Categories

## Factorial (both not recursive and recursive)

``````package main

import (
"fmt"
)
// n! = n * (n-1) * (n-2)...* 2 * 1
// code limited to return value based
// int type interger range
func factorial(num int) int {
if num == 0 {
return 1
}
f := num
for i:=num; i>=3; i-- {
f = f * (i - 1)
}
return f
}

func main() {
// Dn't used value beyond int type integer range
fmt.Println("Factorial:",factorial(20))
}``````
``````package main

import "fmt"

// n! = n * (n-1) * (n-2)...* 2 * 1
// code limited to return value based
// int type interger range
// Recursive function
func fact(n int) int {
// terminate the loop with return
if n == 0 {
return 1
}
// return call fact() function is recursively repeteatly
return n * fact(n - 1)
}

func main() {
fmt.Println(fact(5))
}``````
Categories

## Function closures

Go functions may be closures. A closure is a function value that references variables from outside its body. The function may access and assign to the referenced variables; in this sense the function is “bound” to the variables

``````package main

import "fmt"

sum := 0
return func(x int) int {
sum += x
return sum
}
}

func main() {
for i := 0; i < 10; i++ {
fmt.Println(
pos(i),
neg(-2*i),
)
}
}
``````

https://tour.golang.org/moretypes/25

Categories

## Memoization Technique (Fibonacci)

Let’s utilize a memoization technique to speed up our Fibonacci calculation.

Memoization is an optimization technique used to increase performance by storing the results of expensive function calls and returning the cached result when the same input occurs again.

``````type Memoized func(int) int
var fibMem Memoized``````
Categories

## Fibonacci sequence – a simple recursion and two performance improvements

The Fibonacci sequence is a sequence of numbers where each number is equal to the previous two numbers added together. Here’s an example of this:

`` 1  1  2  3  5  8  13  21  34``

So, 1 plus 1 is 2, 2 plus 3 is 5, 5 plus 8 is 13, and so on.

The Rule is xn = xn−1 + xn−2

where:

• xn is term number “n”
• xn−1 is the previous term (n−1)
• xn−2 is the term before that (n−2)

Let’s use the Fibonacci sequence to help illustrate a number of concepts.

recursive function is a function that calls itself in order to break down complex input into simpler ones.

``````func Fibonacci(x int) int {
if x == 0 {
return 0
} else if x <= 2 {
return 1
} else {
return Fibonacci(x-1) + Fibonacci(x-2)
}
}``````

Try this in Go Playground (restrict to 2 digit number only, otherwise timeout by the Go Playground server.

https://play.golang.org/p/QfhbCWN9KwE

Categories

## Different usage of Function

``````func namedGreeting(name string) {
fmt.Printf("Hey %s!n", name)
}``````
``````func anonymousGreeting() func(string) {
return func(name string) {
fmt.Printf("Hey %s!n", name)
}
}``````
``````func main() {
namedGreeting("Alice")
greet := anonymousGreeting()
greet("Bob")

func(name string) {
fmt.Printf("Hello %s!n", name)
}("Cindy")
}``````