From c127e2898e6f63bdf0733b8c6b636ad9bb32b51c Mon Sep 17 00:00:00 2001 From: PeterBocan Date: Mon, 1 Jul 2019 16:45:01 +0200 Subject: [PATCH 1/3] Added a constructor to structs --- examples/structs/structs.go | 13 ++++++++++ examples/structs/structs.hash | 4 +-- examples/structs/structs.sh | 1 + public/structs | 47 +++++++++++++++++++++++++++++++++-- 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/examples/structs/structs.go b/examples/structs/structs.go index 6cf5629..21584a2 100644 --- a/examples/structs/structs.go +++ b/examples/structs/structs.go @@ -12,6 +12,16 @@ type person struct { age int } +// A de facto constructor of type `person`. +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} + p.age = 42 + return &p +} + func main() { // This syntax creates a new struct. @@ -38,4 +48,7 @@ func main() { // Structs are mutable. 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 2c0e37f..a1194e9 100644 --- a/examples/structs/structs.hash +++ b/examples/structs/structs.hash @@ -1,2 +1,2 @@ -49cad39331ee5e9fb8d8dad99d3aff7f18a4e6d0 -XMZpGsF4sWM +71e8ecdcf8c8fbddbb250ada5bbe8659b68d5229 +nHvnHAGTYHq diff --git a/examples/structs/structs.sh b/examples/structs/structs.sh index 6d82fa0..f0d6025 100644 --- a/examples/structs/structs.sh +++ b/examples/structs/structs.sh @@ -6,3 +6,4 @@ $ go run structs.go Sean 50 51 +&{Jon 42} \ No newline at end of file diff --git a/public/structs b/public/structs index 31c03d0..22fd85f 100644 --- a/public/structs +++ b/public/structs @@ -29,7 +29,7 @@ records.

- +
package main
@@ -66,6 +66,36 @@ records.

+ + +

A de facto constructor of type person.

+ + + + +
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}
+    p.age = 42
+    return &p
+}
+
+ + + + @@ -164,10 +194,23 @@ pointers are automatically dereferenced.

Structs are mutable.

- +
    sp.age = 51
     fmt.Println(sp.age)
+
+ + + + + + +

Call our constructor

+ + + + +
    fmt.Println(NewPerson("Jon"))
 }
 
From 615d5e2eb460851838448c5b1bd7a768b79c9ab9 Mon Sep 17 00:00:00 2001 From: PeterBocan Date: Tue, 2 Jul 2019 15:46:54 +0200 Subject: [PATCH 2/3] Struct allocation fixes. --- examples/structs/structs.go | 8 ++++---- examples/structs/structs.hash | 4 ++-- public/structs | 24 ++++++++++++++++++------ 3 files changed, 24 insertions(+), 12 deletions(-) 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}
From c22f42f03cb1511c24e573374390ec261d6e2704 Mon Sep 17 00:00:00 2001 From: PeterBocan Date: Tue, 2 Jul 2019 16:30:42 +0200 Subject: [PATCH 3/3] Remove extra whitespace --- examples/structs/structs.go | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/structs/structs.go b/examples/structs/structs.go index e2546fe..384b57c 100644 --- a/examples/structs/structs.go +++ b/examples/structs/structs.go @@ -50,5 +50,4 @@ func main() { // Structs are mutable. sp.age = 51 fmt.Println(sp.age) - }