-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1cfb662
commit e9e2779
Showing
44 changed files
with
643 additions
and
641 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -185,25 +185,25 @@ <h2 id="the-newtype-pattern-improves-code-quality"><a class="header" href="#the- | |
<pre><code class="language-go">package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
type Email string | ||
|
||
func newEmail(email string) (Email, error) { | ||
if !strings.Contains(email, "@") { | ||
return Email(""), fmt.Errorf("Expected @ in the email") | ||
if !strings.Contains(email, "@") { | ||
return Email(""), fmt.Errorf("Expected @ in the email") | ||
} | ||
return Email(email), nil | ||
} | ||
|
||
func (email Email) Domain() string { | ||
return strings.Split(string(email), "@")[1] | ||
return strings.Split(string(email), "@")[1] | ||
} | ||
|
||
func main() { | ||
ping, err := newEmail("[email protected]") | ||
ping, err := newEmail("[email protected]") | ||
if err != nil { panic(err) } | ||
fmt.Println(ping.Domain()) | ||
} | ||
|
@@ -226,7 +226,7 @@ <h2 id="when-to-use-value-or-pointer-receiver"><a class="header" href="#when-to- | |
<li>What is the GC behavior of the rest of the program? GC pressure may favor value receivers.</li> | ||
</ul> | ||
<h2 id="parallel-for-loop"><a class="header" href="#parallel-for-loop">Parallel For-Loop</a></h2> | ||
<p>There are two types of for loop on range: "with index" and "without index". Let's see an example for range with index.</p> | ||
<p>There are two types of for loop on range: "with index" and "without index". Let's see an example for range with index.</p> | ||
<pre><code class="language-go">func TestRangeWithIndex(t *testing.T) { | ||
rows := []struct{ index int }{{index: 0}, {index: 1}, {index: 2}} | ||
for _, row := range rows { | ||
|
@@ -248,7 +248,7 @@ <h2 id="parallel-for-loop"><a class="header" href="#parallel-for-loop">Parallel | |
<h3 id="the-same-instance-of-the-variable"><a class="header" href="#the-same-instance-of-the-variable">The same instance of the variable</a></h3> | ||
<p>Since the the loop iterator variable is the same instance of the variable, it may result in tricky error with parallel for-loop.</p> | ||
<pre><code class="language-go">done := make(chan bool) | ||
values := []string{"a", "b", "c"} | ||
values := []string{"a", "b", "c"} | ||
for _, v := range values { | ||
go func() { | ||
fmt.Println(v) | ||
|
@@ -259,7 +259,7 @@ <h3 id="the-same-instance-of-the-variable"><a class="header" href="#the-same-ins | |
<-done | ||
} | ||
</code></pre> | ||
<p>You might expect to see <code>a</code>, <code>b</code>, <code>c</code> as the output, but you'll probably see instead is <code>c</code>, <code>c</code>, <code>c</code>. </p> | ||
<p>You might expect to see <code>a</code>, <code>b</code>, <code>c</code> as the output, but you'll probably see instead is <code>c</code>, <code>c</code>, <code>c</code>.</p> | ||
<p>This is because each iteration of the loop uses the same instance of the variable <code>v</code>, so each closure shares that single variable.</p> | ||
<p>This is the same reason which result wrong test when use <code>t.Parallel()</code> with range, which is covered in <a href="../get-started/write-and-run-unit-tests.html#parallel">Parallel section of Write and run unit tests</a></p> | ||
<h3 id="a-clone-of-iteration-target-value"><a class="header" href="#a-clone-of-iteration-target-value">A clone of iteration target value</a></h3> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.