Skip to content

Commit fa23cff

Browse files
committed
added example of functions being passed as values & how to create methods of structs and defined data types
1 parent 68db215 commit fa23cff

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
lines changed

go_basics/functions/functions.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

3-
import "fmt"
3+
import (
4+
"fmt"
5+
"math"
6+
)
47

58
/*
69
@@ -51,6 +54,43 @@ func showClosures(a int) func(int) (int, error) {
5154
}
5255
}
5356

57+
// functions can be passed as values too
58+
func addWithPi(fn func(int) int, valueToAdd int) int {
59+
return fn(valueToAdd) + int(math.Round(math.Pi))
60+
}
61+
62+
func functionsAsValues(nextElementToAdd int) int {
63+
const RANDOM_VALUE_BASE = 10
64+
funcAsValue := func(a int) int {
65+
return RANDOM_VALUE_BASE + a
66+
}
67+
68+
return addWithPi(funcAsValue, nextElementToAdd)
69+
}
70+
71+
func fibonacci(previousValue int) func() int {
72+
a, b := 0, 1
73+
fmt.Println("a and b at init state", a, b)
74+
return func() int {
75+
result := a
76+
a, b = b, a+b
77+
78+
fmt.Println("a and b", a, b)
79+
80+
return result
81+
}
82+
}
83+
84+
func printFibonacci() {
85+
previousValue := 0
86+
fn := fibonacci(previousValue)
87+
88+
for i := 1; i < 10; i++ {
89+
fmt.Println(fn())
90+
}
91+
92+
}
93+
5494
func main() {
5595
fmt.Println("The result of sumation of two numbers is")
5696
fmt.Println(add(1, 2))
@@ -64,4 +104,10 @@ func main() {
64104
fmt.Println("The result of the closure function")
65105
fmt.Println(showClosures(1)(3))
66106

107+
fmt.Println("Function as value")
108+
fmt.Println(functionsAsValues(24))
109+
110+
fmt.Println("Fibonacci closure")
111+
printFibonacci()
112+
67113
}

go_basics/go.work

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use (
88
./functions
99
./gomaps
1010
./loops
11+
./methods
1112
./numeric_constants
1213
./pointers
1314
./structs

go_basics/methods/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module anarchymonkey.com/go-basics/methods
2+
3+
go 1.20

go_basics/methods/methods.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"math"
7+
)
8+
9+
// as we all know that go does not have any logic of classes embedded in it
10+
// but we can mimic the whole operation by applying the method to a type
11+
12+
type CarParts struct {
13+
doors int
14+
gears int
15+
currentGear int
16+
color string
17+
engineVersion string
18+
}
19+
20+
func (c CarParts) getColorOfCar() string {
21+
return c.color
22+
}
23+
24+
func (c CarParts) getNoOfDoorsInTheCar() int {
25+
return c.doors
26+
}
27+
28+
func (c CarParts) getNoOfGearsInTheCar() int {
29+
return c.gears
30+
}
31+
32+
func (c CarParts) getCurrentGear() int {
33+
return c.currentGear
34+
}
35+
36+
func (c CarParts) changeGear(gearNo int) (int, error) {
37+
38+
if gearNo > c.gears {
39+
return 0, errors.New("the gear to change cannot be more than the no of gears in the car, thus stopping the car")
40+
}
41+
return gearNo, nil
42+
}
43+
44+
// a reciever as a pointer can directly change the values of the underlying structure
45+
func (c *CarParts) addEngineVersion(engineVersion string) {
46+
c.engineVersion = engineVersion
47+
}
48+
49+
type CustomFloat float64
50+
51+
// custom types can also have methods
52+
func (f CustomFloat) getPowerOfFloats(exponent int) float64 {
53+
return float64(math.Pow(float64(f), float64(exponent)))
54+
}
55+
56+
func main() {
57+
fmt.Println("Lets learn about methods")
58+
59+
car := CarParts{
60+
doors: 4,
61+
gears: 6,
62+
color: "Cherry Red",
63+
currentGear: 0,
64+
}
65+
66+
fmt.Printf("The color of the car is %s \n", car.getColorOfCar())
67+
fmt.Printf("The no of doors in the car is %d \n", car.getNoOfDoorsInTheCar())
68+
fmt.Printf("The no of gears in the car is %d \n", car.getNoOfGearsInTheCar())
69+
70+
res, error := car.changeGear(3)
71+
72+
if error != nil {
73+
defer func() {
74+
fmt.Println("The deffered errors are", error)
75+
}()
76+
}
77+
78+
car.currentGear = res
79+
80+
fmt.Println("The current gear after change is", car.getCurrentGear())
81+
82+
car.addEngineVersion("IvtechEngine")
83+
84+
car.addEngineVersion("IVTECH M2")
85+
86+
fmt.Println("The engine version is", car.engineVersion)
87+
88+
var f CustomFloat = CustomFloat(1.234)
89+
90+
fmt.Printf("The floating point value after powering with 2 is %v \n", f.getPowerOfFloats(2))
91+
}

0 commit comments

Comments
 (0)