Struct allocation fixes.

This commit is contained in:
PeterBocan 2019-07-02 15:46:54 +02:00
parent c127e2898e
commit 615d5e2eb4
3 changed files with 24 additions and 12 deletions

View File

@ -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"))
}

View File

@ -1,2 +1,2 @@
71e8ecdcf8c8fbddbb250ada5bbe8659b68d5229
nHvnHAGTYHq
cd504951e9f8504c159a66714b04bfda0629e58c
ezfE4eojTS7

View File

@ -29,7 +29,7 @@ records.</p>
</td>
<td class="code leading">
<a href="http://play.golang.org/p/nHvnHAGTYHq">
<a href="http://play.golang.org/p/ezfE4eojTS7">
<img title="Run code" class="run" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAPCAYAAADtc08vAAAABGdBTUEAANbY1E9YMgAAABl0RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAJ1SURBVCjPY/j//z8DMu7o6GAAgpQgD9tLqcmJH4KDg14aaik/MtdXe2ZjY6OCrh6Fs2jRYmZ9Pd05M9uL/u9dPfU/CLS0dfxvKIz/X5Dg/z8pKdkGqwGpqakMUdExDHJSYqt37tjxf+qUSf9rc2P+79298/+RA3v+H1zV///o6r7/DrbWFQkJiQwxMTGoBjAxMTpKiQmuqMuP/f/xw/v/J0+f/W9tbvTfxVLn/8rJVf+v757z/96hRf8TQtxuCQmLMjk4OKAawMfDVWVvrvd85eTq/7tXTP6/e/XM/22lif9LCnL+b13Q/v/Kzln/L++c/X/7/Jb/VpYWuZFRUagGAAErEBtlxvi+vn944f9L26cDNcz6v21R9/8zm6aC2SBDbu+f/78kK+4/L79AO7oBYCAqxD/57JZp/y/tmPX/wrYZ/6+CbAayD6zs/78daBjIgPayFJAGG6wGAIFAcpjH/dv7F4ANABuya/b/Od3l/ye2V/+/tnv2/7ldxSANmrgMYGBhZg7fuagD7GyYIeeBrrqwdRrQgLn/l02sBGkwwWkAEAjV5EZ/vQV0LswAGAYZsLC3DKTBAJ8BzCkRni/uHFyIYcAtoNc6ypL/ANVIohigrKwMxqqqqgxMzKzM6VHeL+6iGQAKzDtAV5XlJv3n5uFLRTHgzZs3YPzz50+GwqJiPitD9Y8Pjy4BB+CNvfP+3wUmIpAhhckhr3X19LodHZ28UQxQU1MDYw0NDQYBAQEeoBOTK7JjP2xf3Pt/bkfB/4KkoDcKMmIL5OXlFerq6hhu3rzJgC8MwMDYxGSfm5vbVn9/f0cgVxAkpqioyFBfX49iAACbTAK+xT3CzgAAAABJRU5ErkJggg==" />
</a>
<div class="highlight"><pre><span class="kn">package</span> <span class="nx">main</span>
@ -68,7 +68,7 @@ records.</p>
<tr>
<td class="docs">
<p>A de facto constructor of type <code>person</code>.</p>
<p>NewPerson constructs a new person struct with the given name</p>
</td>
<td class="code leading">
@ -160,6 +160,19 @@ as a local variable will survive the scope of the function.</p>
</td>
</tr>
<tr>
<td class="docs">
<p>It&rsquo;s idiomatic to encapsulate new struct creation in constructor functions</p>
</td>
<td class="code leading">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">NewPerson</span><span class="p">(</span><span class="s">&quot;Jon&quot;</span><span class="p">))</span>
</pre></div>
</td>
</tr>
<tr>
<td class="docs">
<p>Access struct fields with a dot.</p>
@ -205,13 +218,11 @@ pointers are automatically dereferenced.</p>
<tr>
<td class="docs">
<p>Call our constructor</p>
</td>
<td class="code">
<div class="highlight"><pre> <span class="nx">fmt</span><span class="p">.</span><span class="nx">Println</span><span class="p">(</span><span class="nx">NewPerson</span><span class="p">(</span><span class="s">&quot;Jon&quot;</span><span class="p">))</span>
<span class="p">}</span>
<div class="highlight"><pre><span class="p">}</span>
</pre></div>
</td>
@ -235,6 +246,7 @@ pointers are automatically dereferenced.</p>
<span class="go">Sean</span>
<span class="go">50</span>
<span class="go">51</span>
<span class="go">&amp;{Jon 42}</span>
</pre></div>
</td>