-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
64 lines (37 loc) · 1.13 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
package main
import (
"fmt"
)
// Difference between methods and functions in golang ?
// A method is a function with a special receiver argument.
// The receiver appears in its own argument list between the func keyword and the method name.
// In this example, the Abs method has a receiver of type Vertex named v.
func main() {
fmt.Println("Welcome to methods in Go!")
// ~ ---------> Creating a struct **********************
lalit := User{"Lalit", "[email protected]", true, 21}
fmt.Println(lalit)
fmt.Printf("lalit details are: %+v\n", lalit)
fmt.Printf("Name is %v and Email is %v\n", lalit.Name, lalit.Email)
// ~ ---------> Creating a method **********************
lalit.GetStatus()
lalit.NewMail()
}
type User struct {
Name string
Email string
Status bool
Age int
}
// ~ ---------> Creating a method **********************
// *General syntax in golang for methods
// func (t Type) methodName(parameter list) {
// code
// }
func (u User) GetStatus() {
fmt.Println("GetStatus method is called on", u.Status)
}
func (u User) NewMail(){
u.Email = "[email protected]"
fmt.Println("Email is changed to", u.Email)
}