mirror of
https://github.com/jbranchaud/til
synced 2026-01-20 07:28:02 +00:00
Compare commits
2 Commits
5e19d53382
...
1ad41b9776
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ad41b9776 | ||
|
|
11716a8fb5 |
@@ -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).
|
||||||
|
|
||||||
_1552 TILs and counting..._
|
_1554 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -406,6 +406,7 @@ _1552 TILs and counting..._
|
|||||||
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
- [Build For A Specific OS And Architecture](go/build-for-a-specific-os-and-architecture.md)
|
||||||
- [Check If Cobra Flag Was Set](go/check-if-cobra-flag-was-set.md)
|
- [Check If Cobra Flag Was Set](go/check-if-cobra-flag-was-set.md)
|
||||||
- [Combine Two Slices](go/combine-two-slices.md)
|
- [Combine Two Slices](go/combine-two-slices.md)
|
||||||
|
- [Connect To A SQLite Database](go/connect-to-a-sqlite-database.md)
|
||||||
- [Create A Slice From An Array](go/create-a-slice-from-an-array.md)
|
- [Create A Slice From An Array](go/create-a-slice-from-an-array.md)
|
||||||
- [Detect If Stdin Comes From A Redirect](go/detect-if-stdin-comes-from-a-redirect.md)
|
- [Detect If Stdin Comes From A Redirect](go/detect-if-stdin-comes-from-a-redirect.md)
|
||||||
- [Deterministically Seed A Random Number Generator](go/deterministically-seed-a-random-number-generator.md)
|
- [Deterministically Seed A Random Number Generator](go/deterministically-seed-a-random-number-generator.md)
|
||||||
@@ -1269,6 +1270,7 @@ _1552 TILs and counting..._
|
|||||||
- [Iterate With An Offset Index](ruby/iterate-with-an-offset-index.md)
|
- [Iterate With An Offset Index](ruby/iterate-with-an-offset-index.md)
|
||||||
- [Include Extra Context In A Honeybadger Notify](ruby/include-extra-context-in-a-honeybadger-notify.md)
|
- [Include Extra Context In A Honeybadger Notify](ruby/include-extra-context-in-a-honeybadger-notify.md)
|
||||||
- [Ins And Outs Of Pry](ruby/ins-and-outs-of-pry.md)
|
- [Ins And Outs Of Pry](ruby/ins-and-outs-of-pry.md)
|
||||||
|
- [Install Latest Version Of Ruby With asdf](ruby/install-latest-version-of-ruby-with-asdf.md)
|
||||||
- [Invoking Rake Tasks Multiple Times](ruby/invoking-rake-tasks-multiple-times.md)
|
- [Invoking Rake Tasks Multiple Times](ruby/invoking-rake-tasks-multiple-times.md)
|
||||||
- [IRB Has Built-In Benchmarking With Ruby 3](ruby/irb-has-built-in-benchmarking-with-ruby-3.md)
|
- [IRB Has Built-In Benchmarking With Ruby 3](ruby/irb-has-built-in-benchmarking-with-ruby-3.md)
|
||||||
- [Jump Out Of A Nested Context With Throw/Catch](ruby/jump-out-of-a-nested-context-with-throw-catch.md)
|
- [Jump Out Of A Nested Context With Throw/Catch](ruby/jump-out-of-a-nested-context-with-throw-catch.md)
|
||||||
|
|||||||
50
go/connect-to-a-sqlite-database.md
Normal file
50
go/connect-to-a-sqlite-database.md
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
# Connect To A SQLite Database
|
||||||
|
|
||||||
|
Using the `database/sql` module and the `github.com/mattn/go-sqlite3` package,
|
||||||
|
we can connect to a SQLite database and run some queries. In my case, I have a
|
||||||
|
SQLite connection string exported to my environment, so I can access that with
|
||||||
|
`os.Getenv`. It's a local SQLite file, `./test.db`.
|
||||||
|
|
||||||
|
Calling `sql.Open`, I'm able to connect with a SQLite3 driver to the database
|
||||||
|
at that connection string. The `setupDatabase` function returns that database
|
||||||
|
connection pointer. Things like `Exec` and `QueryRow` can be called on `db`. I
|
||||||
|
also need to make sure I close the connection to the database with a `defer`.
|
||||||
|
|
||||||
|
Here is a full example of connecting to a local SQLite database and inserting a
|
||||||
|
record:
|
||||||
|
|
||||||
|
```go
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
_ "github.com/mattn/go-sqlite3"
|
||||||
|
)
|
||||||
|
|
||||||
|
func setupDatabase() *sql.DB {
|
||||||
|
databaseString := os.Getenv("GOOSE_DBSTRING")
|
||||||
|
if len(databaseString) == 0 {
|
||||||
|
fmt.Println("Error retrieving `GOOSE_DBSTRING` from env")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
db, err := sql.Open("sqlite3", databaseString)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error opening database: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
return db
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
db := setupDatabase()
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
sql := `insert into users (name) values (?);`
|
||||||
|
|
||||||
|
db.Exec(sql, "Josh")
|
||||||
|
}
|
||||||
|
```
|
||||||
54
ruby/install-latest-version-of-ruby-with-asdf.md
Normal file
54
ruby/install-latest-version-of-ruby-with-asdf.md
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
# Install Latest Version Of Ruby With asdf
|
||||||
|
|
||||||
|
When I check the `asdf` Ruby plugin for known versions of Ruby:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ asdf list-all ruby | fzf
|
||||||
|
```
|
||||||
|
|
||||||
|
I don't find the latest (`3.4`).
|
||||||
|
|
||||||
|
I need to update the plugin. A newer version of the plugin will know about
|
||||||
|
newer Ruby versions.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ asdf plugin-update ruby
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, if I run the `list-all` command again, I'll find the version I'm looking
|
||||||
|
for — `3.4.1`.
|
||||||
|
|
||||||
|
Now that `asdf` and I both know about the version to be installed, I can tell
|
||||||
|
`asdf` to install it:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ asdf install ruby 3.4.1
|
||||||
|
```
|
||||||
|
|
||||||
|
Now, if I check the current Ruby version, I'll see that it is still set to some
|
||||||
|
other version.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ ruby --version
|
||||||
|
ruby 3.2.2 (2023-03-30 revision e51014f9c0) [x86_64-darwin22]
|
||||||
|
```
|
||||||
|
|
||||||
|
I need to tell `asdf` to start using this newly installed version instead,
|
||||||
|
either globally or locally.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ # globally
|
||||||
|
$ asdf global ruby 3.4.1
|
||||||
|
$ # or locally
|
||||||
|
$ asdf local ruby 3.4.1
|
||||||
|
```
|
||||||
|
|
||||||
|
And now I'm all set:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
$ asdf current ruby
|
||||||
|
ruby 3.4.1 /Users/jbranchaud/.tool-versions
|
||||||
|
|
||||||
|
$ ruby --version
|
||||||
|
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-darwin22]
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user