mirror of
https://github.com/jbranchaud/til
synced 2026-01-16 13:38:02 +00:00
Compare commits
4 Commits
bdb119d2de
...
a94b40c673
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a94b40c673 | ||
|
|
464a2af6db | ||
|
|
8801f39df0 | ||
|
|
bc767a0ad3 |
@@ -10,7 +10,7 @@ pairing with smart people at Hashrocket.
|
||||
|
||||
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||
|
||||
_1542 TILs and counting..._
|
||||
_1544 TILs and counting..._
|
||||
|
||||
---
|
||||
|
||||
@@ -412,6 +412,7 @@ _1542 TILs and counting..._
|
||||
- [Not So Random](go/not-so-random.md)
|
||||
- [Parse A String Into Individual Fields](go/parse-a-string-into-individual-fields.md)
|
||||
- [Parse Flags From CLI Arguments](go/parse-flags-from-cli-arguments.md)
|
||||
- [Produce The Zero Value Of A Generic Type](go/produce-the-zero-value-of-a-generic-type.md)
|
||||
- [Redirect File To Stdin During Delve Debug](go/redirect-file-to-stdin-during-delve-debug.md)
|
||||
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.md)
|
||||
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
||||
@@ -1528,6 +1529,7 @@ _1542 TILs and counting..._
|
||||
- [Load Env Vars In Bash Script](unix/load-env-vars-in-bash-script.md)
|
||||
- [Look Through All Files That Have Been Git Stashed](unix/look-through-all-files-that-have-been-git-stashed.md)
|
||||
- [Make Direnv Less Noisy](unix/make-direnv-less-noisy.md)
|
||||
- [Manually Pass Two Git Files To Delta](unix/manually-pass-two-git-files-to-delta.md)
|
||||
- [Map A Domain To localhost](unix/map-a-domain-to-localhost.md)
|
||||
- [Negative Look-Ahead Search With ripgrep](unix/negative-look-ahead-search-with-ripgrep.md)
|
||||
- [Occupy A Local Port With Netcat](unix/occupy-a-local-port-with-netcat.md)
|
||||
|
||||
32
go/produce-the-zero-value-of-a-generic-type.md
Normal file
32
go/produce-the-zero-value-of-a-generic-type.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# Produce The Zero Value For A Generic Type
|
||||
|
||||
While writing a _pop_ function that would work with slices of a generic type, I
|
||||
ran into the issue of needing to produce a zero value of type `T` when
|
||||
returning early for an empty slice.
|
||||
|
||||
The way to arbitrarily get the zero value of a generic in Go is with `*new(T)`.
|
||||
|
||||
I was able to use this in my `Pop` function like so:
|
||||
|
||||
```go
|
||||
func Pop[T any](slice []T) (T, error) {
|
||||
if len(slice) == 0 {
|
||||
return *new(T), fmt.Errorf("cannot pop an empty slice")
|
||||
}
|
||||
|
||||
lastItem := slice[len(slice)-1]
|
||||
|
||||
slice = slice[:len(slice)-1]
|
||||
|
||||
return lastItem, nil
|
||||
}
|
||||
```
|
||||
|
||||
If this is happening in multiple functions and we want a more self-documenting
|
||||
approach, we can pull it out into a function `zero`:
|
||||
|
||||
```go
|
||||
func zero[T any]() T {
|
||||
return *new(T)
|
||||
}
|
||||
```
|
||||
32
unix/manually-pass-two-git-files-to-delta.md
Normal file
32
unix/manually-pass-two-git-files-to-delta.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# Manually Pass Two Git Files To Delta
|
||||
|
||||
I recently [wired up `delta` as my default pager and differ for
|
||||
`git`](git/better-diffs-with-delta.md). However, when I installed `delta`, I
|
||||
first wanted to see what its diff output looked like.
|
||||
|
||||
How can I pass two versions of the same file from `git` to `delta`?
|
||||
|
||||
I can show the current contents of a file with `git show` referencing the
|
||||
`HEAD` commit.
|
||||
|
||||
```bash
|
||||
$ git show HEAD:main.go
|
||||
```
|
||||
|
||||
Similiarly, I can show the contents of that file _one_ commit ago with `HEAD~`.
|
||||
|
||||
```bash
|
||||
$ git show HEAD~:main.go
|
||||
```
|
||||
|
||||
I can then pass each of those commands as virtual files to `delta` using the
|
||||
`<()` syntax. The older file goes first and the newer second.
|
||||
|
||||
```bash
|
||||
$ delta <(git show HEAD~:main.go) <(git show HEAD:main.go)
|
||||
```
|
||||
|
||||
That works and comes in handy if you need to compare two things that aren't
|
||||
necessarily files or aren't necessarily under version control. However, in
|
||||
hindsight, I'd say it is easier to add delta as the pager and differ and try it
|
||||
out directly.
|
||||
@@ -6,7 +6,7 @@ convert it using the `ebook-convert` binary from `Calibre`.
|
||||
First, install `Calibre`:
|
||||
|
||||
```bash
|
||||
$ brew cask install calibre
|
||||
$ brew install --cask calibre
|
||||
```
|
||||
|
||||
Then convert your ePub using `ebook-convert`:
|
||||
|
||||
Reference in New Issue
Block a user