Skip to content

Commit b2bd977

Browse files
author
Rajeev Kumar Singh
committed
Go methods
1 parent e23afa6 commit b2bd977

File tree

17 files changed

+277
-63
lines changed
  • 12-structs
  • 13-methods
    • 01-method-example
    • 02-methods-are-functions
    • 03-same-method-names-on-different-types
    • 04-methods-with-pointer-receivers
    • 05-functions-with-pointer-arguments
    • 06-method-with-pointer-receiver-vs-function-with-pointer-argument
    • 07-method-with-value-receiver-vs-function-with-value-argument

17 files changed

+277
-63
lines changed

12-structs/01-struct-declaration-initialization/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import (
66

77
// Defining a struct type
88
type Person struct {
9-
firstName string
10-
lastName string
11-
age int
9+
FirstName string
10+
LastName string
11+
Age int
1212
}
1313

1414
func main() {
@@ -22,13 +22,13 @@ func main() {
2222

2323
// Naming fields while initializing a struct
2424
p2 := Person{
25-
firstName: "John",
26-
lastName: "Snow",
27-
age: 45,
25+
FirstName: "John",
26+
LastName: "Snow",
27+
Age: 45,
2828
}
2929
fmt.Println("Person2: ", p2)
3030

3131
// Uninitialized fields are set to their corresponding zero-value
32-
p3 := Person{firstName: "Robert"}
32+
p3 := Person{FirstName: "Robert"}
3333
fmt.Println("Person3: ", p3)
3434
}

12-structs/02-struct-access-fields/main.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,23 @@ import (
55
)
66

77
type Car struct {
8-
name, model, color string
9-
weightInKg float64
8+
Name, Model, Color string
9+
WeightInKg float64
1010
}
1111

1212
func main() {
1313
c := Car{
14-
name: "Ferrari",
15-
model: "GTC4",
16-
color: "Red",
17-
weightInKg: 1920,
14+
Name: "Ferrari",
15+
Model: "GTC4",
16+
Color: "Red",
17+
WeightInKg: 1920,
1818
}
1919

2020
// Accessing struct fields using the dot operator
21-
fmt.Println("Car name: ", c.name)
22-
fmt.Println("Car color: ", c.color)
21+
fmt.Println("Car Name: ", c.Name)
22+
fmt.Println("Car Color: ", c.Color)
2323

2424
// Assigning a new value to a struct field
25-
c.color = "Black"
25+
c.Color = "Black"
2626
fmt.Println("Car: ", c)
2727
}

12-structs/03-pointer-to-struct/main.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
)
66

77
type Student struct {
8-
rollNumber int
9-
name string
8+
RollNumber int
9+
Name string
1010
}
1111

1212
func main() {
@@ -18,9 +18,9 @@ func main() {
1818
fmt.Println(ps)
1919

2020
// Accessing struct fields via pointer
21-
fmt.Println((*ps).name)
22-
fmt.Println(ps.name) // Same as above: No need to explicitly dereference the pointer
21+
fmt.Println((*ps).Name)
22+
fmt.Println(ps.Name) // Same as above: No need to explicitly dereference the pointer
2323

24-
ps.rollNumber = 31
24+
ps.RollNumber = 31
2525
fmt.Println(ps)
2626
}

12-structs/04-pointer-to-struct-using-new-function/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ package main
33
import "fmt"
44

55
type Employee struct {
6-
id int
7-
name string
6+
Id int
7+
Name string
88
}
99

1010
func main() {
1111
// You can also get a pointer to a struct using the built-in new() function
1212
// It allocates enough memory to fit a value of the given struct type, and returns a pointer to it
1313
pEmp := new(Employee)
1414

15-
pEmp.id = 1000
16-
pEmp.name = "Sachin"
15+
pEmp.Id = 1000
16+
pEmp.Name = "Sachin"
1717

1818
fmt.Println(pEmp)
1919
}

12-structs/05-nested-struct/main.go

-33
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"github.com/callicoder/golang-tutorials/12-structs/05-struct-exported-vs-unexported/model"
6+
)
7+
8+
func main() {
9+
c := model.Customer{
10+
Id: 1,
11+
Name: "Rajeev Singh",
12+
}
13+
14+
// c.married = true // Error: can not refer to unexported field or method
15+
16+
// a := model.address{} // Error: can not refer to unexported name
17+
18+
fmt.Println("Programmer = ", c);
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package model
2+
3+
// Unexported struct (only accessible inside package `model`)
4+
type address struct {
5+
houseNo, street, city, state, country string
6+
zipCode int
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package model
2+
3+
type Customer struct { // exported struct type
4+
Id int // exported field
5+
Name string // exported field
6+
addr address // unexported field
7+
married bool // unexported field (only accessible inside package `model`)
8+
}

12-structs/06-struct-value-types/main.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package main
33
import "fmt"
44

55
type Point struct {
6-
x float64
7-
y float64
6+
X float64
7+
Y float64
88
}
99

1010
func main() {
@@ -14,7 +14,7 @@ func main() {
1414
fmt.Println("p1 = ", p1)
1515
fmt.Println("p2 = ", p2)
1616

17-
p2.x = 15
17+
p2.X = 15
1818
fmt.Println("\nAfter modifying p2:")
1919
fmt.Println("p1 = ", p1)
2020
fmt.Println("p2 = ", p2)

12-structs/07-struct-equality/main.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ package main
33
import "fmt"
44

55
type Point struct {
6-
x float64
7-
y float64
6+
X float64
7+
Y float64
88
}
99

1010
func main() {

13-methods/01-method-example/main.go

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
// Struct type - `Circle`
9+
type Circle struct {
10+
R float64
11+
}
12+
13+
// Method with receiver `Circle`
14+
func (c Circle) Area() float64 {
15+
return math.Pi * c.R * c.R;
16+
}
17+
18+
func main() {
19+
c := Circle{5.0}
20+
21+
fmt.Println("Circle : ", c)
22+
fmt.Println("Area of Circle : ", c.Area())
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
// Struct type - `Circle`
9+
type Circle struct {
10+
R float64
11+
}
12+
13+
// Function to calculate the area of Circle
14+
func Area(c Circle) float64 {
15+
return math.Pi * c.R * c.R
16+
}
17+
18+
/*
19+
Compare the above function with the corresponding method -
20+
21+
func (c Circle) Area() float64 {
22+
return math.Pi * c.R * c.R;
23+
}
24+
*/
25+
26+
func main() {
27+
c := Circle{5.0}
28+
29+
fmt.Println("Circle : ", c)
30+
fmt.Println("Area of Circle : ", Area(c))
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math"
6+
)
7+
8+
// Struct type - `Circle`
9+
type Circle struct {
10+
R float64
11+
}
12+
13+
// Struct type - `Rectangle`
14+
type Rectangle struct {
15+
Length, Width float64
16+
}
17+
18+
// Method with receiver `Circle`
19+
func (c Circle) Area() float64 {
20+
return math.Pi * c.R * c.R;
21+
}
22+
23+
// Method with receiver `Rectangle`
24+
func (r Rectangle) Area() float64 {
25+
return r.Length * r.Width;
26+
}
27+
28+
func main() {
29+
c := Circle{5.0}
30+
r := Rectangle{7.0, 4.0}
31+
32+
fmt.Println("Circle : ", c)
33+
fmt.Println("Area of Circle : ", c.Area())
34+
35+
fmt.Println("Rectangle : ", r)
36+
fmt.Println("Area of Rectangle : ", r.Area())
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Point struct {
8+
X, Y float64
9+
}
10+
11+
/*
12+
Translates the current Point, at location (X,Y), by dx along the x axis and dy along the y axis
13+
so that it now represents the point (X+dx,Y+dy).
14+
*/
15+
func (p *Point) Translate(dx, dy float64) {
16+
p.X = p.X + dx
17+
p.Y = p.Y + dy
18+
}
19+
20+
func main() {
21+
p := Point{3, 4}
22+
fmt.Println("Point p = ", p)
23+
24+
p.Translate(7, 9)
25+
fmt.Println("After Translate, p = ", p)
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type Point struct {
8+
X, Y float64
9+
}
10+
11+
/*
12+
Translates the current Point, at location (X,Y), by dx along the x axis and dy along the y axis
13+
so that it now represents the point (X+dx,Y+dy).
14+
*/
15+
func TranslateFunc(p *Point, dx, dy float64) {
16+
p.X = p.X + dx
17+
p.Y = p.Y + dy
18+
}
19+
20+
func main() {
21+
p := Point{3, 4}
22+
fmt.Println("Point p = ", p)
23+
24+
TranslateFunc(&p, 7, 9)
25+
fmt.Println("After Translate, p = ", p)
26+
}

0 commit comments

Comments
 (0)