From a8230c3a627f26836e77b4001c7c182beb0a9d5d Mon Sep 17 00:00:00 2001 From: Ritesh Harjani Date: Sun, 28 Jun 2020 11:27:24 +0530 Subject: [PATCH 1/5] go: slices: How to allocate 2 dimensional slice for problem --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index 20ba33d..c30e3e8 100644 --- a/README.md +++ b/README.md @@ -409,6 +409,15 @@ a = make([]byte, 5) // capacity is optional // create a slice from an array x := [3]string{"Лайка", "Белка", "Стрелка"} s := x[:] // a slice referencing the storage of x + +// allocating a 2 dimensional slice for problem Exercise: Slices +func Pic(dx, dy int) [][]uint8 { + mat := make([][]uint8, dy) + for i := 0; i < dy; i++ { + mat[i] = make([]uint8, dx) + } + return mat +} ``` ### Operations on Arrays and Slices From c9778e671f21d323746518e982d18edfb09f4178 Mon Sep 17 00:00:00 2001 From: Ritesh Harjani Date: Sun, 28 Jun 2020 12:05:50 +0530 Subject: [PATCH 2/5] go: function: example of showing function passed as argument value --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index c30e3e8..2e6d41c 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,23 @@ func main() { fmt.Println(add(3, 4)) } +// this shows function passed as argument in another function +//Functions are values too. They can be passed around just like other values. +//Function values may be used as function arguments and return values. +func compute(fn func(float64, float64) float64) float64 { + return fn(3, 4) +} + +func main() { + hypot := func(x, y float64) float64 { + return math.Sqrt(x*x + y*y) + } + fmt.Println(hypot(5, 12)) + + fmt.Println(compute(hypot)) + fmt.Println(compute(math.Pow)) +} + // Closures, lexically scoped: Functions can access values that were // in scope when defining the function func scope() func() int{ From 7b5515adc01dcbd2845f6d492d7c801567b09fd4 Mon Sep 17 00:00:00 2001 From: Ritesh Harjani Date: Sun, 28 Jun 2020 12:16:10 +0530 Subject: [PATCH 3/5] go: function: Add some more examples of function closure 1. Add information that the function is bound to it's own closure reference variable defined in it's outer scope. 2. Add another example of function closure from go tour --- README.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/README.md b/README.md index 2e6d41c..35b5f7e 100644 --- a/README.md +++ b/README.md @@ -212,6 +212,10 @@ func another_scope() func() int{ } +//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. +//For example, the outer() function returns a closure. Each closure is bound to its own `outer_var` variable. + // Closures func outer() (func() int, int) { outer_var := 2 @@ -222,6 +226,36 @@ func outer() (func() int, int) { inner() return inner, outer_var // return inner func and mutated outer_var 101 } + +// another example would be an "adder()" function (Taken from go tour) +func adder() func(int) int { + sum := 0 + return func(x int) int { + sum += x + return sum + } +} + +func main() { + pos, neg := adder(), adder() + for i := 0; i < 10; i++ { + fmt.Println( + pos(i), + neg(-2*i), + ) + } +} +Output:- +0 0 +1 -2 +3 -6 +6 -12 +10 -20 +15 -30 +21 -42 +28 -56 +36 -72 +45 -90 ``` ### Variadic Functions From bdc06313287676589b24f8915f1eae456c0f31a9 Mon Sep 17 00:00:00 2001 From: Ritesh Harjani Date: Sun, 28 Jun 2020 17:09:31 +0530 Subject: [PATCH 4/5] go: function closure: Add an example of return Fibonacci closure --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index 35b5f7e..7c7e5ac 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,38 @@ Output:- 28 -56 36 -72 45 -90 + +//e.g from https://tour.golang.org/moretypes/26 +package main +import "fmt" + +// fibonacci is a function that returns +// a function that returns an int. +func fibonacci() func() int { + f1, f2 := 0, 1 + return func() int { + f := f1 + f1, f2 = f2, f1 + f2 + return f + } +} +func main() { + f := fibonacci() + for i := 0; i < 10; i++ { + fmt.Println(f()) + } +} +Output:- +0 +1 +1 +2 +3 +5 +8 +13 +21 +34 ``` ### Variadic Functions From ad0a8d14d0ab5cf3f4974a376633d9c6e80b1e08 Mon Sep 17 00:00:00 2001 From: Ritesh Harjani Date: Sun, 28 Jun 2020 17:17:38 +0530 Subject: [PATCH 5/5] go: methods: e.g. methods on non-struct types (.e.g. float64) --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 7c7e5ac..18e5746 100644 --- a/README.md +++ b/README.md @@ -589,6 +589,33 @@ func (v *Vertex) add(n float64) { v.Y += n } + +//You can declare a method on non-struct types, too. +//In this example we see a numeric type MyFloat with an Abs method. +//You can only declare a method with a receiver whose type is defined in the same package as the method. +//You cannot declare a method with a receiver whose type is defined in another package (which includes the built-in types such as int). + +package main + +import ( + "fmt" + "math" +) + +type MyFloat float64 + +func (f MyFloat) Abs() float64 { + if f < 0 { + return float64(-f) + } + return float64(f) +} + +func main() { + f := MyFloat(-math.Sqrt2) + fmt.Println(f.Abs()) +} + ``` **Anonymous structs:** Cheaper and safer than using `map[string]interface{}`.