Python dataclasses are awesome!

Last week, I was working on some Python at work and I found myself wishing for an equivalent to Golang’s structs. Rather than passing in a bunch of core data types (string, dict, int, etc.) in an out of methods, I wanted to pass in a single object with predictable attributes. But…. I didn’t want to go through all the trouble of creating a class with a constructor, and passing in all the various attributes....

2023-05-22

nil maps in Golang

I’ve been a bit confused for a while over when a map in Golang gets created with a value of nil (its zero value) and when it does not, so I’m writing this to help me remember. Let’s look at various ways to initialize a map: package main import "fmt" func main() { m1 := map[string]string{} // initializes map m2 := make(map[string]string) // also initializes map var m3 map[string]string // does NOT initialize the map!...

2023-01-07

Thoughts on travel to Hawaii

My wife and I recently went to the Hawaiin islands for a few days for a vacatoin. Here’s what worked well and didn’t well about the trip. Resorts We stayed in resorts for the first time. This was new; normally we stay in Airbnbs or the like. Pros: Consistent experience. Clean and nice. Pools and hottubs. Safe. We felt very good leaving laptops and the like in the room. Cons:...

2022-11-08

Built-in Go HTTP server

Go has a built-in HTTP server in net/http. Here’s me playing around using it: package main import ( "fmt" "net/http" "strings" ) func hello(w http.ResponseWriter, req *http.Request) { fmt.Fprintf(w, "hello\n") } func details(w http.ResponseWriter, req *http.Request) { resp := []string{} resp = append(resp, "Request Details:") resp = append(resp, fmt.Sprintf("- Request proto: %s", req.Proto)) resp = append(resp, fmt.Sprintf("- Headers:")) for name, val := range req.Header { resp = append(resp, fmt.Sprintf("\t- %s: %s", name, val)) } resp = append(resp, "\n") fmt....

2022-10-14

Fixing: File is not `goimports`-ed (goimports)

The other day, I was linting my Go code with golangci-lint when I got this error: File is not `goimports`-ed (goimports) I examined the file and found nothing amiss, but the linter insisted something was wrong. Eventually, I realized I had used spaces to indent the file rather than tabs. Changing the indent character to tabs fixed it. I then ran into the same error on a second file. This time, I had a comment which was wrong....

2022-10-05

Linux load average

This post documents the high level concepts I need to remember about *nix Load Average. Load average can been seen in the output of uptime and top: From uptime: $ uptime 14:40 up 21 days, 21:48, 5 users, load averages: 2.63 2.75 3.45 From top: root@blog-server-2:~# top -n 1 | head -2 top - 15:28:37 up 10 days, 12:41, 1 user, load average: 0.04, 0.03, 0.00 Tasks: 95 total, 1 running, 94 sleeping, 0 stopped, 0 zombie root@blog-server-2:~# There are 3 decimal numbers shown....

2022-09-16

Using entr to get immediate test feedback

Old and new workflows My dev workflow used to look like this: Write some code and save it Change to my Terminal window Up-arrow to find my command to run unit tests (typically make test) Press Enter Wait for tests to run and examine test output This worked pretty well, but was a lot of manual steps when you figure that I did this dozens (hundreds?) of times per day. That’s a lot of energy spent doing the same thing over and over again....

2022-09-07

Go and pass by value

Go normally uses pass-by-value for function calls. When you pass a variable into a function or method, Go will (under the hood) create a new variable, copy the old variable’s value into it, and then pass the new variable (not the original variable) into the function or method. Non-pointer values These types behave as described above and are sometimes called non-pointer values: Strings Ints Floats Booleans Arrays Structs Here’s an example of how these work....

2022-09-01

Constants in Go

Constants syntax I always have a hard time remembering the syntax of declaring constants in Go. So here’s some reminders to myself. It is possible to declare constants one per line like you’d expect. Note that the there are typed and untyped constants as shown here below. package main import "fmt" const vermKnid string = "scram" const fox = "foxes likes loxes" func main() { fmt.Println(vermKnid) fmt.Println(fox) } But you can also use a constant declaration group, which does the same thing but is easier to read if you’re declaring a bunch of constants....

2022-08-16

Open a file or folder from the macOS CLI

You can open the a file from the macOS CLI with the command open. To open a file with the default app, use: open $FILENAME To open the current directory, use: open . To open a specific directory, use open $DIRECTORY_NAME To open a URL, use: open $URL Thanks to this post for teaching me this.

2022-08-09