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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 9 additions & 9 deletions
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
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+
}
Lines changed: 7 additions & 0 deletions
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+
}
Lines changed: 8 additions & 0 deletions
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 2 additions & 2 deletions
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() {

0 commit comments

Comments
 (0)