mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 15:38:02 +00:00
Compare commits
1 Commits
52339c8146
...
b4040e10e6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4040e10e6 |
@@ -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).
|
For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186).
|
||||||
|
|
||||||
_1527 TILs and counting..._
|
_1521 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -402,8 +402,6 @@ _1527 TILs and counting..._
|
|||||||
- [Do Something N Times](go/do-something-n-times.md)
|
- [Do Something N Times](go/do-something-n-times.md)
|
||||||
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
- [Find Executables Installed By Go](go/find-executables-installed-by-go.md)
|
||||||
- [Not So Random](go/not-so-random.md)
|
- [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)
|
|
||||||
- [Replace The Current Process With An External Command](go/replace-the-current-process-with-an-external-command.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)
|
- [Sleep For A Duration](go/sleep-for-a-duration.md)
|
||||||
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
- [Upgrading From An Older Version On Mac](go/upgrading-from-an-older-version-on-mac.md)
|
||||||
@@ -459,7 +457,6 @@ _1527 TILs and counting..._
|
|||||||
- [Get Random Images From Unsplash](internet/get-random-images-from-unsplash.md)
|
- [Get Random Images From Unsplash](internet/get-random-images-from-unsplash.md)
|
||||||
- [Search Tweets By Author](internet/search-tweets-by-author.md)
|
- [Search Tweets By Author](internet/search-tweets-by-author.md)
|
||||||
- [Show All Pivotal Stories With Blockers](internet/show-all-pivotal-stories-with-blockers.md)
|
- [Show All Pivotal Stories With Blockers](internet/show-all-pivotal-stories-with-blockers.md)
|
||||||
- [Verify Site Ownership With DNS Record](internet/verify-site-ownership-with-dns-record.md)
|
|
||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
@@ -576,7 +573,6 @@ _1527 TILs and counting..._
|
|||||||
### jj
|
### jj
|
||||||
|
|
||||||
- [Colocate jj And git Directories For Project](jj/colocate-jj-and-git-directories-for-project.md)
|
- [Colocate jj And git Directories For Project](jj/colocate-jj-and-git-directories-for-project.md)
|
||||||
- [Find System-wide Config File For User](jj/find-system-wide-config-file-for-user.md)
|
|
||||||
|
|
||||||
### jq
|
### jq
|
||||||
|
|
||||||
@@ -640,7 +636,6 @@ _1527 TILs and counting..._
|
|||||||
- [Specify App When Opening From Command Line](mac/specify-app-when-opening-from-command-line.md)
|
- [Specify App When Opening From Command Line](mac/specify-app-when-opening-from-command-line.md)
|
||||||
- [Use Default Screenshot Shortcuts With CleanShot X](mac/use-default-screenshot-shortcuts-with-cleanshot-x.md)
|
- [Use Default Screenshot Shortcuts With CleanShot X](mac/use-default-screenshot-shortcuts-with-cleanshot-x.md)
|
||||||
- [View All Windows Of The Current App](mac/view-all-windows-of-the-current-app.md)
|
- [View All Windows Of The Current App](mac/view-all-windows-of-the-current-app.md)
|
||||||
- [Write System Clipboard To A File](mac/write-system-clipboard-to-a-file.md)
|
|
||||||
|
|
||||||
### MongoDB
|
### MongoDB
|
||||||
|
|
||||||
@@ -779,7 +774,6 @@ _1527 TILs and counting..._
|
|||||||
- [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md)
|
- [Escaping String Literals With Dollar Quoting](postgres/escaping-string-literals-with-dollar-quoting.md)
|
||||||
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
|
- [Export Query Results To A CSV](postgres/export-query-results-to-a-csv.md)
|
||||||
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
- [Extracting Nested JSON Data](postgres/extracting-nested-json-data.md)
|
||||||
- [Fetch Specific Number Of Results](postgres/fetch-specific-number-of-results.md)
|
|
||||||
- [Find Duplicate Records In Table Without Unique Id](postgres/find-duplicate-records-in-table-without-unique-id.md)
|
- [Find Duplicate Records In Table Without Unique Id](postgres/find-duplicate-records-in-table-without-unique-id.md)
|
||||||
- [Find Records That Contain Duplicate Values](postgres/find-records-that-contain-duplicate-values.md)
|
- [Find Records That Contain Duplicate Values](postgres/find-records-that-contain-duplicate-values.md)
|
||||||
- [Find Records That Have Multiple Associated Records](postgres/find-records-that-have-multiple-associated-records.md)
|
- [Find Records That Have Multiple Associated Records](postgres/find-records-that-have-multiple-associated-records.md)
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
# Parse A String Into Individual Fields
|
|
||||||
|
|
||||||
Let's say you're reading in data from a file or otherwise dealing with an
|
|
||||||
arbitrary string of data. If that string has a series of values separated by
|
|
||||||
whitespace, you can parse it into individual fields with
|
|
||||||
[`strings.Fields`](https://pkg.go.dev/strings#Fields).
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
data := "3 5 2 6 7 1 9"
|
|
||||||
fields := strings.Fields(data)
|
|
||||||
|
|
||||||
fmt.Printf("Fields: %v", fields)
|
|
||||||
// [3 5 2 6 7 1 9]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
Here is another example where we can see that `strings.Fields` deals with
|
|
||||||
multiple whitespace and surrounding whitespace:
|
|
||||||
|
|
||||||
```go
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
data := " go java c++ rust "
|
|
||||||
fields := strings.Fields(data)
|
|
||||||
|
|
||||||
fmt.Printf("%v", fields)
|
|
||||||
// [go java c++ rust]
|
|
||||||
}
|
|
||||||
```
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
# Parse Flags From CLI Arguments
|
|
||||||
|
|
||||||
Though we can grab the arguments to a Go program from `os.Args`, it requires
|
|
||||||
some manual parsing. With the built-in `flag` package, we can declare specific
|
|
||||||
flags our program accepts, by type. When we parse them, they will be separated
|
|
||||||
out from the rest of the positional arguments.
|
|
||||||
|
|
||||||
Here is an example of the program that accepts a boolean `debug` flag. This
|
|
||||||
will work with either `-debug` or `--debug`.
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
var debug bool
|
|
||||||
flag.BoolVar(&debug, "debug", false, "turns on debug mode, extra logging")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
positionalArgs := flag.Args()
|
|
||||||
|
|
||||||
if len(positionalArgs) < 1 {
|
|
||||||
fmt.Println("Please specify which part to run: 1 or 2")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
if debug {
|
|
||||||
fmt.Println("We are in debug mode...")
|
|
||||||
fmt.Println("Received the following argument:", positionalArgs[0])
|
|
||||||
}
|
|
||||||
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
We can run the program in debug mode like so:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go run . --debug 123
|
|
||||||
We are in debug mode...
|
|
||||||
Received the following argument: 123
|
|
||||||
```
|
|
||||||
|
|
||||||
We can also take advantage of the `help` flag that we get for free:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go run . --help
|
|
||||||
Usage of /var/folders/62/lx9pcjbs1zbd83zg6twwym2r0000gn/T/go-build3212087168/b001/exe/test:
|
|
||||||
-debug
|
|
||||||
turns on debug mode, extra logging
|
|
||||||
```
|
|
||||||
|
|
||||||
Note: any recognized flags need to come before any of the position arguments.
|
|
||||||
The `debug` flag won't be picked up if we run the program like this:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ go run . 123 --debug
|
|
||||||
```
|
|
||||||
|
|
||||||
[source](https://pkg.go.dev/flag)
|
|
||||||
@@ -1,28 +0,0 @@
|
|||||||
# Verify Site Ownership With DNS Record
|
|
||||||
|
|
||||||
To run your site through Google Search Console and get detailed reports, you
|
|
||||||
need to verify that you own the site. There are several manual ways of doing
|
|
||||||
this that involve sticking a value unique to your URL in a file or header tag.
|
|
||||||
There is a better way though.
|
|
||||||
|
|
||||||
By adding a TXT DNS record wherever you domain's DNS is managed, you can prove
|
|
||||||
to Google that you own the domain. That verification applies to all paths and
|
|
||||||
subdomains of that domain.
|
|
||||||
|
|
||||||
Some providers like Cloudflare have a mostly-automated process for this that
|
|
||||||
Google can hook into as long as you grant permission via OAuth.
|
|
||||||
|
|
||||||
You can also manually create the TXT record if necessary.
|
|
||||||
|
|
||||||
Either way, it will look something like:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ dig -t TXT visualmode.dev
|
|
||||||
|
|
||||||
;; ANSWER SECTION:
|
|
||||||
visualmode.dev. 377 IN TXT "google-site-verification=MBZ2S2fhnh2gHRxFniRrYW-O6mdyimJDRFj-f
|
|
||||||
vblwtk"
|
|
||||||
```
|
|
||||||
|
|
||||||
More details are provided in the [Google Search Console
|
|
||||||
docs](https://support.google.com/webmasters/answer/9008080?hl=en#domain_name_verification).
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
# Find System-wide Config File For User
|
|
||||||
|
|
||||||
The `jj` CLI can be configured in a couple different places. When I recently
|
|
||||||
ran a `jj config` command, I was curious where specifically it was getting set.
|
|
||||||
Those changes didn't appear in the repo's config (`./.jj/repo/config.toml`).
|
|
||||||
That makes sense since it would only apply to that repo. So, where is the
|
|
||||||
system-wide config file?
|
|
||||||
|
|
||||||
The following commond shows where on your machine it is located.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ jj config path --user
|
|
||||||
/Users/jbranchaud/Library/Application Support/jj/config.toml
|
|
||||||
```
|
|
||||||
|
|
||||||
Now, the next time I set a config like this:
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ jj config set --user ui.paginate never
|
|
||||||
```
|
|
||||||
|
|
||||||
or want to check what other config options are set to, I can visit that path
|
|
||||||
and take a look.
|
|
||||||
|
|
||||||
[source](https://github.com/martinvonz/jj/blob/main/docs/config.md)
|
|
||||||
@@ -1,19 +0,0 @@
|
|||||||
# Write System Clipboard To A File
|
|
||||||
|
|
||||||
MacOS has two CLI utilities `pbcopy` and `pbpaste` which, respectively, copy
|
|
||||||
_to_ and paste _from_ the system clipboard via the CLI.
|
|
||||||
|
|
||||||
Let's say I've just copied a large block of text from somewhere onto my system
|
|
||||||
clipboard. I now want to paste that into a new file. Instead of creating a new
|
|
||||||
file, opening it up in my preferred editor, pasting all that text, and saving
|
|
||||||
the file, I can run one small command from the CLI.
|
|
||||||
|
|
||||||
```bash
|
|
||||||
$ pbpaste > data.txt
|
|
||||||
```
|
|
||||||
|
|
||||||
This redirects the contents of `pbpaste` (which is the system clipboard) into
|
|
||||||
the file `data.txt`. If that file doesn't already exist, then it will be
|
|
||||||
created before the data is written to it.
|
|
||||||
|
|
||||||
See `man pbpaste` for more details.
|
|
||||||
@@ -1,42 +0,0 @@
|
|||||||
# Fetch Specific Number Of Results
|
|
||||||
|
|
||||||
If you pull up just about any intro to PostgreSQL (or even SQL), one of the
|
|
||||||
first things they are going to teach you is the `limit` clause. This is taught
|
|
||||||
as _the_ way for limiting the result set to a specific number of rows.
|
|
||||||
|
|
||||||
```sql
|
|
||||||
> select title from books limit 4;
|
|
||||||
+-----------------------+
|
|
||||||
| title |
|
|
||||||
|-----------------------|
|
|
||||||
| The Secret History |
|
|
||||||
| A Gentleman in Moscow |
|
|
||||||
| Exhalation: Stores |
|
|
||||||
| Annihilation |
|
|
||||||
+-----------------------+
|
|
||||||
SELECT 4
|
|
||||||
```
|
|
||||||
|
|
||||||
You might be as surprised as I was to learn that `limit` is not part of the SQL
|
|
||||||
standard. It is extremely common for this use case, but the SQL standard
|
|
||||||
defines `fetch first N rows only` as the way to fetch a specific number of
|
|
||||||
rows. As we can see, [it works identically to `limit
|
|
||||||
N`](https://www.postgresql.org/docs/current/sql-select.html#SQL-LIMIT).
|
|
||||||
|
|
||||||
```sql
|
|
||||||
> select title from books fetch first 4 rows only;
|
|
||||||
+-----------------------+
|
|
||||||
| title |
|
|
||||||
|-----------------------|
|
|
||||||
| The Secret History |
|
|
||||||
| A Gentleman in Moscow |
|
|
||||||
| Exhalation: Stores |
|
|
||||||
| Annihilation |
|
|
||||||
+-----------------------+
|
|
||||||
SELECT 4
|
|
||||||
```
|
|
||||||
|
|
||||||
The `rows` and `row` keywords are interchangeable which makes statements more
|
|
||||||
readable if, for instance, you're doing `... fetch first 1 row only`.
|
|
||||||
|
|
||||||
[source](https://www.cybertec-postgresql.com/en/postgresql-limit-vs-fetch-first-rows-with-ties/)
|
|
||||||
Reference in New Issue
Block a user