Skip to content

Commit 993a07e

Browse files
author
Rajeev Kumar Singh
committed
Go Structs and Code cleanup
1 parent d7a6a38 commit 993a07e

File tree

12 files changed

+54
-17
lines changed

12 files changed

+54
-17
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
// You can create a pointer using the built-in new() function
7+
ptr := new(int) // Pointer to an int
8+
*ptr = 100
9+
10+
fmt.Printf("Ptr = %#x, Ptr value = %d\n", ptr, *ptr)
11+
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,23 @@ type Person struct {
1212
}
1313

1414
func main() {
15-
// Creating an instance of the 'person' struct type
15+
// Declaring a variable of a `struct` type
16+
var p Person // // All the struct fields are initialized with their zero value
17+
fmt.Println(p)
18+
19+
// Declaring and initializing a struct using a struct literal
1620
p1 := Person{"Rajeev", "Singh", 26}
1721
fmt.Println("Person1: ", p1)
1822

19-
// Naming fields while initializing the struct
23+
// Naming fields while initializing a struct
2024
p2 := Person{
2125
firstName: "John",
2226
lastName: "Snow",
2327
age: 45,
2428
}
2529
fmt.Println("Person2: ", p2)
2630

27-
// Uninitialized fields are assigned their zero-value
28-
p3 := Person{
29-
firstName: "Robert",
30-
}
31+
// Uninitialized fields are set to their corresponding zero-value
32+
p3 := Person{firstName: "Robert"}
3133
fmt.Println("Person3: ", p3)
3234
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func main() {
1717
weightInKg: 1920,
1818
}
1919

20-
// Accessing struct fields using dot notation
20+
// Accessing struct fields using the dot operator
2121
fmt.Println("Car name: ", c.name)
2222
fmt.Println("Car color: ", c.color)
2323

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
// instance of student struct type
1414
s := Student{11, "Jack"}
1515

16-
// Pointer to a student struct
16+
// Pointer to the student struct
1717
ps := &s
1818
fmt.Println(ps)
1919

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type Employee struct {
6+
id int
7+
name string
8+
}
9+
10+
func main() {
11+
// You can also get a pointer to a struct using the built-in new() function
12+
// It allocates enough memory to fit a value of the given struct type, and returns a pointer to it
13+
pEmp := new(Employee)
14+
15+
pEmp.id = 1000
16+
pEmp.name = "Sachin"
17+
18+
fmt.Println(pEmp)
19+
}

10-structs/04-nested-struct/main.go renamed to 10-structs/05-nested-struct/main.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,17 @@ type Customer struct {
1616
}
1717

1818
func main() {
19-
c := Customer{name: "Rajeev Singh", age: 25}
20-
c.address = Address{
21-
houseNo: "747",
22-
street: "Golf View Road",
23-
city: "Seattle",
24-
state: "Washington",
25-
country: "United States",
26-
zipCode: 98101,
19+
c := Customer{
20+
name: "Rajeev Singh",
21+
age: 25,
22+
address: Address{
23+
houseNo: "747",
24+
street: "Golf View Road",
25+
city: "Seattle",
26+
state: "Washington",
27+
country: "United States",
28+
zipCode: 98101,
29+
},
2730
}
2831

2932
fmt.Println("Customer: ", c)

0 commit comments

Comments
 (0)