Skip to content

Commit d0fe7e6

Browse files
committed
arithmetic package in funcname workspace
1 parent 0b7b299 commit d0fe7e6

File tree

7 files changed

+76
-0
lines changed

7 files changed

+76
-0
lines changed

funcname/arithmetic/add.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package arithmetic
2+
3+
// Add function will take two ints and return addition of int
4+
// when two parameters are of same type, they can be grouped like this
5+
func Add(a, b int) int {
6+
return a + b
7+
}

funcname/arithmetic/all.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package arithmetic
2+
3+
// Do method takes two operands and perform all 4 arithmetic operators
4+
// you can also have types associated separately in parameters like this
5+
func Do(a int, b int) (int, int, int, float32, error) {
6+
// since div function return two values
7+
// it needs to be deconstructed before return
8+
div, err := Div(a, b)
9+
10+
// Add, Sub, Prod can be directly used as
11+
return Add(a, b), Sub(a, b), Prod(a, b), div, err
12+
}

funcname/arithmetic/div.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package arithmetic
2+
3+
import "errors"
4+
5+
func Div(a, b int) (float32, error) {
6+
if b == 0 {
7+
// divide by zero is undefined
8+
// if user provided this value, then send 0 and error
9+
return 0, errors.New("divide by zero error")
10+
}
11+
12+
// since division can be fractional, convert operands to float32()
13+
// if b != 0 then we can safely divide and return value
14+
// error interface is allowed store and return nil values
15+
// only pointers and error can be assigned to nil
16+
return float32(a) / float32(b), nil
17+
}

funcname/arithmetic/prod.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package arithmetic
2+
3+
// Prod retutns multiplication return of two ints
4+
func Prod(a, b int) int {
5+
return a * b
6+
}

funcname/arithmetic/sub.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package arithmetic
2+
3+
func Sub(a, b int) int {
4+
return a - b
5+
}

funcname/main.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
11
package main
22

33
import (
4+
"fmt"
45
"os"
56

7+
// since this is imported before init, this package init() will be called firs
8+
9+
"funcname/arithmetic"
610
"funcname/otherpackage"
711
)
812

13+
var i = 10
14+
15+
// called before main()
16+
func init() {
17+
fmt.Println("Initialization function for main")
18+
i = 20
19+
}
20+
921
func main() {
1022
// call the exported function
1123
otherpackage.Greet(os.Args[1:])
24+
25+
// this value will be from init()
26+
fmt.Println("Value of I", i)
27+
28+
fmt.Println("-- Arithmetic Demo --")
29+
fmt.Println(arithmetic.Add(10, 20))
30+
fmt.Println(arithmetic.Sub(10, 20))
31+
fmt.Println(arithmetic.Prod(10, 20))
32+
fmt.Println(arithmetic.Div(10, 20))
33+
fmt.Println(arithmetic.Div(10, 0))
34+
fmt.Println(arithmetic.Do(3, 4))
35+
fmt.Println(arithmetic.Do(3, 0))
1236
}

funcname/otherpackage/greet.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ package otherpackage
22

33
import "fmt"
44

5+
// called before init of main package
6+
func init() {
7+
fmt.Println("Initialization function for otherpackage")
8+
}
9+
510
// greetname will print hello to name
611
func greetname(name string) {
712
fmt.Printf("Hello, %s\n", name)

0 commit comments

Comments
 (0)