my net house

WAHEGURU….!

Dive into Interfaces in Golang.

How to Think about interfaces in Golang.

1. Yo need to package multiple Functions into one Interface. That’s we also call Polymorphism as well.
2. Then the data on which you need to Apply those Operations Should be a Struct{}
3. Bingo! Now you know what are the interfaces.

Note* : Make sure the Funcitons You will be creating in the interaces shold be having Function Declaration like this:

func (u user) notify() {
fmt.Printf(“Sending User Email To %s\n”,
u.name,
u.email)
}

Where “user” is struct.

A dead Simple Explanation is as Follows:

package main

import "fmt"

// notifier is an interface that defines notification
// type behavior.
type notifier interface {
notify()
}

// user defines a user in the program.
type user struct {
name string
email string
}

// notify implements the notifier interface with a pointer receiver.
func (u user) notify() {
fmt.Printf("Sending User Email To %s\n",
u.name,
u.email)
}

func main() {

// Create a value of type User and send a notification.
u := user{"Bill", "bill@email.com"}
m := user{"Arshpreet", "arsh840@gmail.com"}
// Values of type user do not implement the interface because pointer
// receivers don't belong to the method set of a value.

u.notify()
m.notify()
}

Using Type Assertion For Interface. Why to use Type Assertion;

https://stackoverflow.com/questions/38816843/explain-type-assertions-in-go

package main

import "fmt"
import "math"

func main() {
shapes := []Shape{
Circle{1.0},
Square{1.772453},
Rectangle{5, 10},
Triangle{10, 4, 7},
}
for _, v := range shapes {
fmt.Println(v, "\tArea:", v.Area())
if t, ok := v.(Triangle); ok {
fmt.Println("Angles:", t.Angles())
}
}
}

type Shape interface {
Area() float64
}
type Circle struct {
Radius float64
}
type Triangle struct {
A, B, C float64 // lengths of the sides of a triangle.
}
type Rectangle struct {
A, B float64
}
type Square struct {
A float64
}

func (t Circle) Area() float64 {
return math.Pi * t.Radius * t.Radius
}

// Heron's Formula for the area of a triangle
func (t Triangle) Area() float64 {
p := (t.A + t.B + t.C) / 2.0 // perimeter half
return math.Sqrt(p * (p - t.A) * (p - t.B) * (p - t.C))
}
func (t Rectangle) Area() float64 {
return t.A * t.B
}

func (t Square) Area() float64 {
return t.A * t.A
}

func (t Circle) String() string {
return fmt.Sprint("Circle (Radius: ", t.Radius, ")")
}
func (t Triangle) String() string {
return fmt.Sprint("Triangle (Sides: ", t.A, ", ", t.B, ", ", t.C, ")")
}
func (t Rectangle) String() string {
return fmt.Sprint("Rectangle (Sides: ", t.A, ", ", t.B, ")")
}
func (t Square) String() string {
return fmt.Sprint("Square (Sides: ", t.A, ")")
}

func (t Triangle) Angles() []float64 {
return []float64{angle(t.B, t.C, t.A), angle(t.A, t.C, t.B), angle(t.A, t.B, t.C)}
}
func angle(a, b, c float64) float64 {
return math.Acos((a*a+b*b-c*c)/(2*a*b)) * 180.0 / math.Pi
}

Emty interaces are the Most Amazing thing you will Love about Golang, If you Come from Python Background.

package main

import "fmt"

func main() {

// fmt.Println can be called with values of any type.
fmt.Println("Hello, world")
fmt.Println(12345)
fmt.Println(3.14159)
fmt.Println(true)
var a interface{}
a = "hello"
fmt.Println("this is a", a)
a = 23453535
fmt.Println("this is a", a)
a = []int{2, 3, 5, 7, 11, 13}
fmt.Println("this is a", a)

}

Sample program to show the syntax and mechanics of type switches and the empty interface.

package main

import "fmt"

func main() {

// fmt.Println can be called with values of any type.
fmt.Println("Hello, world")
fmt.Println(12345)
fmt.Println(3.14159)
fmt.Println(true)

// How can we do the same?
myPrintln("Hello, world")
myPrintln(12345)
myPrintln(3.14159)
myPrintln(true)

}

func myPrintln(a interface{}) {
switch v := a.(type) {
case string:
fmt.Printf("Is string : type(%T) : value(%s)\n", v, v)
case int:
fmt.Printf("Is int : type(%T) : value(%d)\n", v, v)
case float64:
fmt.Printf("Is float64 : type(%T) : value(%f)\n", v, v)
default:
fmt.Printf("Is unknown : type(%T) : value(%v)\n", v, v)
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: