diff --git a/examples/structs/structs.go b/examples/structs/structs.go index 21584a2..e2546fe 100644 --- a/examples/structs/structs.go +++ b/examples/structs/structs.go @@ -12,9 +12,8 @@ type person struct { age int } -// A de facto constructor of type `person`. +// NewPerson constructs a new person struct with the given name func NewPerson(name string) *person { - // You can safely return a pointer to local variable // as a local variable will survive the scope of the function. p := person{name: name} @@ -36,6 +35,9 @@ func main() { // An `&` prefix yields a pointer to the struct. fmt.Println(&person{name: "Ann", age: 40}) + // It's idiomatic to encapsulate new struct creation in constructor functions + fmt.Println(NewPerson("Jon")) + // Access struct fields with a dot. s := person{name: "Sean", age: 50} fmt.Println(s.name) @@ -49,6 +51,4 @@ func main() { sp.age = 51 fmt.Println(sp.age) - // Call our constructor - fmt.Println(NewPerson("Jon")) } diff --git a/examples/structs/structs.hash b/examples/structs/structs.hash index a1194e9..50e4d67 100644 --- a/examples/structs/structs.hash +++ b/examples/structs/structs.hash @@ -1,2 +1,2 @@ -71e8ecdcf8c8fbddbb250ada5bbe8659b68d5229 -nHvnHAGTYHq +cd504951e9f8504c159a66714b04bfda0629e58c +ezfE4eojTS7 diff --git a/public/structs b/public/structs index 22fd85f..0ea36aa 100644 --- a/public/structs +++ b/public/structs @@ -29,7 +29,7 @@ records.
package main
@@ -68,7 +68,7 @@ records.
- A de facto constructor of type person
.
+ NewPerson constructs a new person struct with the given name
@@ -160,6 +160,19 @@ as a local variable will survive the scope of the function.
+
+
+ It’s idiomatic to encapsulate new struct creation in constructor functions
+
+
+
+
+ fmt.Println(NewPerson("Jon"))
+
+
+
+
+
Access struct fields with a dot.
@@ -205,13 +218,11 @@ pointers are automatically dereferenced.
- Call our constructor
-
+
- fmt.Println(NewPerson("Jon"))
-}
+ }
@@ -235,6 +246,7 @@ pointers are automatically dereferenced.
Sean
50
51
+&{Jon 42}