diff --git a/circle/circle.go b/circle/circle.go new file mode 100644 index 0000000..e028d16 --- /dev/null +++ b/circle/circle.go @@ -0,0 +1,28 @@ +package circle + +import ( + "fmt" +) + +type Circle interface { + Area() + Perimeter() +} + +type circ struct { + radius int +} + +func NewCircle(r int) Circle { + return &circ{ + radius: r, + } +} + +func (c *circ) Area() { + fmt.Println("Area of Circle : ", (22/7)*c.radius*c.radius) +} + +func (c *circ) Perimeter() { + fmt.Println("Perimeter of Circle : ", 2*(22/7)*c.radius) +} diff --git a/cmd/interface/main.go b/cmd/interface/main.go new file mode 100644 index 0000000..84c91f8 --- /dev/null +++ b/cmd/interface/main.go @@ -0,0 +1,15 @@ +package main + +import ( + "github.com/utkarsh-josh/interface/circle" + "github.com/utkarsh-josh/interface/rectangle" +) + +func main() { + rect := rectangle.NewRectangle(5, 10) + circ := circle.NewCircle(14) + rect.Area() + rect.Perimeter() + circ.Area() + circ.Perimeter() +} diff --git a/cmd/switch/main.go b/cmd/switch/main.go new file mode 100644 index 0000000..6a13540 --- /dev/null +++ b/cmd/switch/main.go @@ -0,0 +1,30 @@ +package main + +import ( + "fmt" +) + +func testDataType(variable interface{}) { + switch variable.(type) { + case string: + fmt.Println("It is a string : ", variable.(string)) + case int: + fmt.Println("It is an integer : ", variable.(int)) + case float64: + fmt.Println("It is a float : ", variable.(float64)) + default: + fmt.Println("Data type not present") + } +} + +func main() { + var variable interface{} + variable = "Utkarsh" + testDataType(variable) + variable = 10 + testDataType(variable) + variable = 99.9 + testDataType(variable) + variable = []string{} + testDataType(variable) +} diff --git a/cmd/type_assertion/main.go b/cmd/type_assertion/main.go new file mode 100644 index 0000000..713c788 --- /dev/null +++ b/cmd/type_assertion/main.go @@ -0,0 +1,41 @@ +package main + +import ( + "fmt" +) + +func testString(variable interface{}) { + if val, ok := variable.(string); ok { + fmt.Println("It is a string : ", val) + return + } + fmt.Println("It is not a string") +} + +func testInteger(variable interface{}) { + if val, ok := variable.(int); ok { + fmt.Println("It is an integer : ", val) + return + } + fmt.Println("It is not an integer") +} + +func testFloat(variable interface{}) { + if val, ok := variable.(float64); ok { + fmt.Println("It is a float : ", val) + return + } + fmt.Println("It is not a float") +} + +func main() { + var variable interface{} + variable = "Utkarsh" + testString(variable) + variable = 10 + testInteger(variable) + variable = 99.9 + testFloat(variable) + testString(variable) + testInteger(variable) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..49502aa --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/utkarsh-josh/interface + +go 1.18 diff --git a/rectangle/rectangle.go b/rectangle/rectangle.go new file mode 100644 index 0000000..7325144 --- /dev/null +++ b/rectangle/rectangle.go @@ -0,0 +1,30 @@ +package rectangle + +import ( + "fmt" +) + +type Rectangle interface { + Area() + Perimeter() +} + +type rect struct { + length int + breadth int +} + +func NewRectangle(l, b int) Rectangle { + return &rect{ + length: l, + breadth: b, + } +} + +func (r *rect) Area() { + fmt.Println("Area of rectangle : ", r.length*r.breadth) +} + +func (r *rect) Perimeter() { + fmt.Println("Perimiter of rectangle : ", 2*(r.length+r.breadth)) +}