File tree 2 files changed +34
-1
lines changed
2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
10
10
11
11
For a steady stream of TILs, [ sign up for my newsletter] ( https://crafty-builder-6996.ck.page/e169c61186 ) .
12
12
13
- _ 1543 TILs and counting..._
13
+ _ 1544 TILs and counting..._
14
14
15
15
---
16
16
@@ -412,6 +412,7 @@ _1543 TILs and counting..._
412
412
- [ Not So Random] ( go/not-so-random.md )
413
413
- [ Parse A String Into Individual Fields] ( go/parse-a-string-into-individual-fields.md )
414
414
- [ Parse Flags From CLI Arguments] ( go/parse-flags-from-cli-arguments.md )
415
+ - [ Produce The Zero Value Of A Generic Type] ( go/produce-the-zero-value-of-a-generic-type.md )
415
416
- [ Redirect File To Stdin During Delve Debug] ( go/redirect-file-to-stdin-during-delve-debug.md )
416
417
- [ Replace The Current Process With An External Command] ( go/replace-the-current-process-with-an-external-command.md )
417
418
- [ Sleep For A Duration] ( go/sleep-for-a-duration.md )
Original file line number Diff line number Diff line change
1
+ # Produce The Zero Value For A Generic Type
2
+
3
+ While writing a _ pop_ function that would work with slices of a generic type, I
4
+ ran into the issue of needing to produce a zero value of type ` T ` when
5
+ returning early for an empty slice.
6
+
7
+ The way to arbitrarily get the zero value of a generic in Go is with ` *new(T) ` .
8
+
9
+ I was able to use this in my ` Pop ` function like so:
10
+
11
+ ``` go
12
+ func Pop [T any](slice []T ) (T, error ) {
13
+ if len (slice) == 0 {
14
+ return *new (T), fmt.Errorf (" cannot pop an empty slice" )
15
+ }
16
+
17
+ lastItem := slice[len (slice)-1 ]
18
+
19
+ slice = slice[:len (slice)-1 ]
20
+
21
+ return lastItem, nil
22
+ }
23
+ ```
24
+
25
+ If this is happening in multiple functions and we want a more self-documenting
26
+ approach, we can pull it out into a function ` zero ` :
27
+
28
+ ``` go
29
+ func zero[T any]() T {
30
+ return *new (T)
31
+ }
32
+ ```
You can’t perform that action at this time.
0 commit comments