Skip to content

Commit d7a6a38

Browse files
author
Rajeev Kumar Singh
committed
Go Structs
1 parent 4d302a1 commit d7a6a38

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

10-structs/05-struct-equality/main.go

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Point struct {
6+
x float64
7+
y float64
8+
}
9+
10+
func main() {
11+
// Structs are value types.
12+
p1 := Point{10, 20}
13+
p2 := p1 // A copy of the struct `p1` is assigned to `p2`
14+
fmt.Println("p1 = ", p1)
15+
fmt.Println("p2 = ", p2)
16+
17+
p2.x = 15
18+
fmt.Println("\nAfter modifying p2:")
19+
fmt.Println("p1 = ", p1)
20+
fmt.Println("p2 = ", p2)
21+
}

10-structs/06-struct-equality/main.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Point struct {
6+
x float64
7+
y float64
8+
}
9+
10+
func main() {
11+
// Two structs are equal if all their corresponding fields are equal.
12+
p1 := Point{3.4, 5.2}
13+
p2 := Point{3.4, 5.2}
14+
15+
if p1 == p2 {
16+
fmt.Println("Point p1 and p2 are equal.")
17+
} else {
18+
fmt.Println("Point p1 and p2 are not equal.")
19+
}
20+
}

0 commit comments

Comments
 (0)