Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions circle/circle.go
Original file line number Diff line number Diff line change
@@ -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)
}
15 changes: 15 additions & 0 deletions cmd/interface/main.go
Original file line number Diff line number Diff line change
@@ -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()
}
30 changes: 30 additions & 0 deletions cmd/switch/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
41 changes: 41 additions & 0 deletions cmd/type_assertion/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/utkarsh-josh/interface

go 1.18
30 changes: 30 additions & 0 deletions rectangle/rectangle.go
Original file line number Diff line number Diff line change
@@ -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))
}