-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
186 lines (136 loc) · 4.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package main
import (
"fmt"
)
// Define an interface
type Speaker interface {
Speak() string
}
// Define a struct
type Dog struct {
Name string
}
// Implement the Speak method for the Dog type
func (d Dog) Speak() string {
return "Woof!"
}
// Define a Cat struct
type Cat struct {
Name string
}
// Implement the Speak method for Cat
func (c Cat) Speak() string {
return "Meow!"
}
// Function that accepts any type
func PrintValue(val interface{}) {
fmt.Println(val)
}
func PrintValue1(val interface{}) {
// Type assertion to extract the underlying value as an int
if v, ok := val.(int); ok {
fmt.Printf("Integer value: %d\n", v)
} else {
fmt.Println("Not an integer")
}
}
func PrintValue2(val interface{}) {
// Type switch to handle different types
switch v := val.(type) {
case int:
fmt.Printf("Integer value: %d\n", v)
case string:
fmt.Printf("String value: %s\n", v)
case float64:
fmt.Printf("Float64 value: %f\n", v)
default:
fmt.Println("Unknown type")
}
}
// Define an interface with multiple methods
type Vehicle interface {
Drive() string
Horn() string
}
// Define a Car struct
type Car struct {
Brand string
}
// Implement the Vehicle interface for Car
func (c Car) Drive() string {
return "Driving the car"
}
func (c Car) Horn() string {
return "Beep beep!"
}
// Define an interface
type Flyer interface {
Fly() string
}
// Define a struct with a method that satisfies the Flyer interface
type Bird struct {
Species string
}
func (b Bird) Fly() string {
return "Flying high!"
}
// Define another struct that embeds Bird and adds new methods
type Eagle struct {
Bird
}
func (e Eagle) Hunt() string {
return "Hunting prey!"
}
// Implement the HuntPrey method that accepts a prey name as a parameter
func (e Eagle) HuntPrey(prey string) string {
return fmt.Sprintf("The eagle is hunting a %s!", prey)
}
func main() {
fmt.Println("-----------------------------------------------------------------------------------")
// Basic Interface Declaration
// Create an instance of Dog
dog := Dog{Name: "Buddy"}
// Declare a variable of type Speaker and assign it to a Dog instance
var speaker Speaker = dog
// Call the Speak method
fmt.Println(speaker.Speak()) // Output: Woof!
fmt.Println("-----------------------------------------------------------------------------------")
// Multiple Types Implementing the Same Interface
cat := Cat{Name: "Whiskers"}
var speaker2 Speaker = cat
fmt.Println(speaker2.Speak()) // Output: Meow!
fmt.Println("-----------------------------------------------------------------------------------")
// Empty Interface (interface{})
// Using the PrintValue function with different types
PrintValue(42) // Output: 42
PrintValue("Hello, Go!") // Output: Hello, Go!
PrintValue(3.14) // Output: 3.14
fmt.Println("-----------------------------------------------------------------------------------")
// Type Assertion
// Type assertion is a way to extract the underlying value of an interface
PrintValue1(42) // Output: Integer value: 42
PrintValue1("Hello, Go!") // Output: Not an integer
fmt.Println("-----------------------------------------------------------------------------------")
// Type Switch
PrintValue(42) // Output: Integer value: 42
PrintValue("Hello, Go!") // Output: String value: Hello, Go!
PrintValue(3.14) // Output: Float64 value: 3.140000
fmt.Println("-----------------------------------------------------------------------------------")
// Interfaces with Multiple Methods
// Create an instance of Car
car := Car{Brand: "Toyota"}
// Declare a variable of type Vehicle and assign it to a Car instance
var vehicle Vehicle = car
// Call the Drive and Horn methods
fmt.Println(vehicle.Drive()) // Output: Driving the car
fmt.Println(vehicle.Horn()) // Output: Beep beep!
fmt.Println("-----------------------------------------------------------------------------------")
// Interfaces with Multiple Methods
// Create an instance of Eagle
eagle := Eagle{Bird{Species: "Eagle"}}
// The Eagle can use methods from Bird as well as its own methods
fmt.Println(eagle.Fly()) // Output: Flying high!
fmt.Println(eagle.Hunt()) // Output: Hunting prey!
fmt.Println(eagle.HuntPrey("rabbit")) // Output: The eagle is hunting a rabbit!
fmt.Println("-----------------------------------------------------------------------------------")
}