Repository: elliotchance/pie
Pull request #153 (2019-05-25 02:04:41)

#152 fix zero value of pointer is not nil
see [152](https://github.com/elliotchance/pie/issues/152)

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/elliotchance/pie/153)
<!-- Reviewable:end -->


codecov-io: # [Codecov](https://codecov.io/gh/elliotchance/pie/pull/153?src=pr&el=h1) Report
> :exclamation: No coverage uploaded for pull request base (`master@db87529`). [Click here to learn what that means](https://docs.codecov.io/docs/error-reference#section-missing-base-commit).
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/elliotchance/pie/pull/153/graphs/tree.svg?width=650&token=OvugwUQGM3&height=150&src=pr)](https://codecov.io/gh/elliotchance/pie/pull/153?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##             master   #153   +/-   ##
=======================================
  Coverage          ?   100%           
=======================================
  Files             ?      8           
  Lines             ?   1643           
  Branches          ?      0           
=======================================
  Hits              ?   1643           
  Misses            ?      0           
  Partials          ?      0
```


| [Impacted Files](https://codecov.io/gh/elliotchance/pie/pull/153?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [pie/carpointers\_pie.go](https://codecov.io/gh/elliotchance/pie/pull/153/diff?src=pr&el=tree#diff-cGllL2NhcnBvaW50ZXJzX3BpZS5nbw==) | `100% <100%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/elliotchance/pie/pull/153?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/elliotchance/pie/pull/153?src=pr&el=footer). Last update [db87529...bc39dc2](https://codecov.io/gh/elliotchance/pie/pull/153?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/main.go b/main.go
index 5882ae7..a82331d 100644
--- a/main.go
+++ b/main.go
@@ -210,7 +210,7 @@ func main() {
 			// If its a pointer we need to replace '*' -> '&' when
 			// instantiating.
 			if elementType[0] == '*' {
-				zeroValue = "&" + zeroValue[1:]
+				zeroValue = "nil"
 			}
 
 			t = strings.Replace(t, "ElementZeroValue", zeroValue, -1)
diff --git a/pie/carpointers_pie.go b/pie/carpointers_pie.go
index fe4990f..93d92c7 100755
--- a/pie/carpointers_pie.go
+++ b/pie/carpointers_pie.go
@@ -234,7 +234,7 @@ func (ss carPointers) FindFirstUsing(fn func(value *car) bool) int {
 
 // First returns the first element, or zero. Also see FirstOr().
 func (ss carPointers) First() *car {
-	return ss.FirstOr(&car{})
+	return ss.FirstOr(nil)
 }
 
 // FirstOr returns the first element or a default value if there are no
@@ -348,7 +348,7 @@ func (ss carPointers) JSONStringIndent(prefix, indent string) string {
 
 // Last returns the last element, or zero. Also see LastOr().
 func (ss carPointers) Last() *car {
-	return ss.LastOr(&car{})
+	return ss.LastOr(nil)
 }
 
 // LastOr returns the last element or a default value if there are no elements.
@@ -420,7 +420,7 @@ func (ss carPointers) Random(source rand.Source) *car {
 
 	// Avoid the extra allocation.
 	if n < 1 {
-		return &car{}
+		return nil
 	}
 	if n < 2 {
 		return ss[0]


Test patch:
diff --git a/pie/carpointers_test.go b/pie/carpointers_test.go
index 4771347..8cf680d 100644
--- a/pie/carpointers_test.go
+++ b/pie/carpointers_test.go
@@ -114,9 +114,9 @@ var carPointersFirstAndLastTests = []struct {
 }{
 	{
 		nil,
-		&car{},
+		nil,
 		&car{"default1", "unknown"},
-		&car{},
+		nil,
 		&car{"default2", "unknown"},
 	},
 	{
@@ -762,17 +762,17 @@ var carPointersRandomTests = []struct {
 }{
 	{
 		nil,
-		&car{},
+		nil,
 		nil,
 	},
 	{
 		nil,
-		&car{},
+		nil,
 		rand.NewSource(0),
 	},
 	{
 		carPointers{},
-		&car{},
+		nil,
 		rand.NewSource(0),
 	},
 	{