Batch Word (.doc) tables to Excel on macOS — AppleScript and pandas

Tips-如何批量将Word表格转为Excel

Tips: all for hands-free.

This series is a log of small tricks that save time day to day.

One goal: don’t let repetitive chores numb you — automate when you can.


Today: batch-convert Word (.doc) tables to Excel on a Mac.

Preview:

Batch convert Word (.doc) tables to Excel

The first time you pick a folder, macOS asks you to grant access.
Word save-as runs in the background; the GIF doesn’t show each open/save/close cycle, but Word does flash on screen.

Read More

Dig101 — struct memory alignment in Go

Dig101-Go之聊聊struct的内存对齐

Update: slides from Go Night Reading #588


Dig101: dig more, simplify more, know more

Strings, slices, and maps all lean on structs under the hood. Here: struct memory alignment — handy when reading stdlib code and shaving wasted padding.

Read More

Dig101 — map internals (hmap, buckets, growth, iteration)

Dig101:Go之读懂map的底层设计

Dig101: dig more, simplify more, know more

Maps are everywhere in Go. They hide hashing, collision chains, growth, and compaction. Below is the runtime layout (Go 1.14beta1 on macOS) without a line-by-line source dump.

Read More

Dig101 — Go slice header, slicing tricks, and append growth

Dig101-Go之灵活的slice

Dig101: dig more, simplify more, know more.

Slices are everywhere in Go. Unlike fixed-size arrays, a slice is a small header over a variable-length backing array.

0x01 What is a slice?

Runtime definition:

1
2
3
4
5
6
// src/runtime/slice.go
type slice struct {
array unsafe.Pointer
len int
cap int
}

array points at the backing store; len is length; cap is capacity. Declare a, inspect the header, and print %p for &a, sh, a, and &a[0]:

1
2
3
4
5
6
7
8
a := make([]int, 1, 3)
// reflect.SliceHeader mirrors the runtime layout (unsafe for production code)
sh := (*reflect.SliceHeader)(unsafe.Pointer(&a))
fmt.Printf("slice header: %#v\naddress of a: %p &a[0]: %p | &a: %p sh:%p ",
sh, a, &a[0],&a, sh)

//slice header: &reflect.SliceHeader{Data:0xc000018260, Len:1, Cap:3}
//address of a: 0xc000018260 &a[0]: 0xc000018260 | &a: 0xc00000c080 sh:0xc00000c080

a and &a[0] match—the slice “value” for printing is the first element’s address. &a, sh, and the header’s storage address match—that is the slice variable on the stack, not the array blob. Keeping those two addresses straight helps with later puzzles.

Read More

Go for-range pitfalls — loop vars, copies, and maps

Dig101-Go之for-range排坑指南

Dig101 is back — digging into mechanics behind everyday Go.

Two common iteration forms: for and for-range. Range has sharp edges — the usual traps below.

Update: Go 1.22 gives each iteration its own loop variables, so the classic “shared loop var” bugs below do not apply on 1.22+ unless you use an older release.

0x01 Can you collect pointers to every element?

1
2
3
4
5
6
7
8
arr := [2]int{1, 2}
res := []*int{}
for _, v := range arr {
res = append(res, &v)
}
//expect: 1 2
fmt.Println(*res[0],*res[1])
//but output: 2 2

No — same surprise on []int{1, 2} or map[int]int{1:1, 2:2}.

Read More

Go sync.Mutex — normal mode, starvation mode, and the runtime wait queue

golang的Mutex锁如何实现

Overview

sync.Mutex is a semaphore-style lock: acquire decrements availability; release increments and may wake one waiter.

Layout:

1
2
3
4
5
6
7
8
9
10
11
type Mutex struct {
state int32 // low bits: locked, woken, starving; upper bits: waiter count
sema uint32
}
const (
mutexLocked = 1 << iota // mutex is locked
mutexWoken
mutexStarving
mutexWaiterShift = iota
starvationThresholdNs = 1e6 // starvation after ~1ms wait
)

Starving mode avoids goroutines stuck behind endless barging.

  1. Normal mode — FIFO wait queue, but a woken waiter races newcomers; newcomers often win because they are already on-CPU.

  2. Starvation mode — after ~1 ms waiting, newcomers queue only (no spin, no steal). Unlock hands the lock directly to the front waiter.

  3. Back to normal when wait time < 1 ms or the wait queue is empty.

Read More

Go runtime memory allocation — heap, mcache, and tcmalloc

go源码分析之内存分配

Go runtime memory pool

Overview

Go manages memory for you: a heap allocator plus the GC. Most of the time you never touch malloc yourself. Below: the pool allocator in the runtime (Go 1.10.3) and how the pieces connect.

The design follows tcmalloc (thread-caching malloc): per-thread caches for small objects (≤32 KiB), then central lists, then the heap, then the OS. Tiered caches shrink lock contention and fragmentation.

Below: how memory is carved up, the main structs, then the allocation path in source.

Read More

Go heap vs stack — escape analysis with slices

go学习之堆栈中的变量

Stack versus heap allocation in Go stayed fuzzy until I read the articles in the references at the end. Here is a condensed version.

What the Go FAQ says

How do I know whether a variable is allocated on the heap or the stack?

From a correctness standpoint, you do not need to know. Each variable in Go exists as long as there are references to it. The storage location chosen by the implementation is irrelevant to the semantics of the language.

The storage location does affect efficient code. When possible, the Go compilers allocate variables that are local to a function in that function’s stack frame. If the compiler cannot prove that the variable is not referenced after the function returns, it must allocate on the garbage-collected heap to avoid dangling pointer errors. If a local variable is very large, the heap may be a better choice than the stack.

In current compilers, taking the address of a variable makes heap allocation likely. Escape analysis can still keep some of those variables on the stack when they clearly do not outlive the function.

Read More