12 Aug 2025
在 go 專案下要 build bin 檔的時候出現 error :
34.21 go: xxxx.com/qson/libraries/qlib@v0.2.73: unrecognized import path "xxxx.com/yy/libraries/qlib": https fetch: Get "https://xxxx.com/yy/libraries/lib?go-get=1": dial tcp 122.17.60.101:443: connect: no route to host
------
Dockerfile:34
--------------------
33 | COPY go.* .
34 | >>> RUN --mount=type=secret,id=netrc,dst=/root/.netrc \
35 | >>> --mount=type=cache,target=/go/pkg/mod \
36 | >>> --mount=type=cache,target=/root/.cache/go-build \
37 | >>> go install github.com/swaggo/swag/cmd/swag@latest && \
38 | >>> go install github.com/t-yuki/gocover-cobertura@latest && \
39 | >>> go install gotest.tools/gotestsum@latest && \
40 | >>> go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && \
41 | >>> go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest && \
42 | >>> go mod download
43 |
--------------------
ERROR: failed to solve: process "/bin/sh -c go install github.com/swaggo/swag/cmd/swag@latest && go install github.com/t-yuki/gocover-cobertura@latest && go install gotest.tools/gotestsum@latest && go install google.golang.org/protobuf/cmd/protoc-gen-go@latest && go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest && go mod download" did not complete successfully: exit code: 1
這是由於 go.mod 中有一個私有模組,但 Docker 容器在 go mod download 的時候連不到內部伺服器,所以整個步驟失敗。
Solution
- 在可以連到內部伺服器的主機上先:
go mod vendor
,然後 Dockerfile 改成:
COPY go.* .
COPY vendor/ vendor/
RUN go mod vendor && \
go install ...
這樣 build 時不需要再上網抓私有套件。
- 把 go mod download 刪掉
什麼是 go mod vendor
- 平常 Go build 會去網路下載依賴(go mod download),這時如果有私有模組、外網斷線,就會失敗。用 go mod vendor 把依賴先放進 vendor/ 後,Go build 可以直接從 vendor/ 讀取,不必再連網抓套件,適合離線建置或有內部私有依賴的情況。
- Go 1.14+ 開始,go build、go install 會自動偵測 vendor/ 並使用它(除非用 -mod=readonly 或 -mod=mod 強制不用)。
- 缺點:vendor 資料夾可能很大、若有新版本需要重新 go mod vendor
14 Jul 2025
用 systemctl start xx.service
啟動某個 daemon 的時候,出現下面錯誤:
EXT4-fs (sde3): ext4_check_descriptors: Checksum for group 0 failed (9679!=59862)
EXT4-fs (sde3): group descriptors corrupted!
這表示 /dev/sde3 分區的 ext4 superblock 或 group descriptor 出現校驗錯誤(corruption)。
修復方式
執行
umount /dev/sde3 # 如果已掛載
e2fsck -f /dev/sde3
這會掃描並修復損壞的 metadata(會詢問是否修復,建議按 y)
如果你看到類似 “Inode bitmap checksums invalid” 等錯誤,也會自動修!
29 Jun 2025
題目描述
You are given an array of integers nums and an integer target.
Return the number of non-empty subsequences of nums such that the sum of the minimum and maximum element on it is less or equal to target. Since the answer may be too large, return it modulo 10^9 + 7.
範例
Example 1:
Input: nums = [3,5,6,7], target = 9
Output: 4
Explanation: There are 4 subsequences that satisfy the condition.
[3] -> Min value + max value <= target (3 + 3 <= 9)
[3,5] -> (3 + 5 <= 9)
[3,5,6] -> (3 + 6 <= 9)
[3,6] -> (3 + 6 <= 9)
Example 2:
Input: nums = [3,3,6,8], target = 10
Output: 6
Explanation: There are 6 subsequences that satisfy the condition. (nums can have repeated numbers).
[3] , [3] , [3,3], [3,6] , [3,6] , [3,3,6]
### Example 3:
Input: nums = [2,3,3,4,6,7], target = 12
Output: 61
Explanation: There are 63 non-empty subsequences, two of them do not satisfy the condition ([6,7], [7]).
Number of valid subsequences (63 - 2 = 61).
Constraints:
- 1 <= nums.length <= 10^5
- 1 <= nums[i] <= 10^6
- 1 <= target <= 10^6
解法
two poiner
- sort (由小到大)
- two pointer 一個從左一個從右邊找到 arr[i] + arr[j] <= k
- 如果 arr[i] + arr[j] <= k 代表最小值還能增加,移動左指針,反之移動右指針,讓 arr[i] + arr[j] 變小
- 計算這個區間中的 subsequence 有多少,加到答案裡面
func numSubseq(nums []int, target int) int {
const MOD = 1_000_000_007
sort.Ints(nums)
n := len(nums)
pow2 := make([]int, n+1)
pow2[0] = 1
for i:=1; i<=n; i++ {
pow2[i] = pow2[i-1]*2 % MOD
}
res := 0
l, r := 0, n-1
for l <= r {
if nums[l] + nums[r] <= target {
res += pow2[r-l]
res %= MOD
l++
} else {
r--
}
}
return res
}
計算子集合
從 nums[l+1] 到 nums[r] 這段長度為 r - l 中挑選任意個元素(可以不挑),總共會有 2^(r - l) 個子集合(不包含 nums[l])
為啥不包含 nums[l] ?
這些子集合的前提是我們已經固定選了 nums[l](因為我們要它當「最小值」)。固定選 nums[l](最小值),然後從 nums[l+1..r] 這段中挑選 0~(r-l) 個元素,這 2^(r - l) 個子序列都是合法的。
為啥不能包含 nums[l] ?
- 如果你把 nums[l] 也當成「可選或不選」的一部分,那就會錯,因為會重複計算那些不包含 nums[l] 的子序列
- 那些子序列可能會有最小值更大的元素,這違反我們當前判斷是以 nums[l] 為「最小值」的條件
11 Jun 2025
遇到錯誤 Error loading key “/root/.ssh/repo_key”: error in libcrypto ,卡了非常久,試過 chatgpt 建議的各種方法。
最後發現是 private key 檔案後必須要有 newline…
18 Apr 2025
ARP(Address Resolution Protocol,地址解析協定)是一種網路協定,用來在區域網路中將 IP 位址解析為實體的 MAC 位址。
ARP(有時稱為第 2 層,因為它正在更新底層網路拓撲)為了到達某個 IP 位址,讓流量發送到特定的硬體。 ARP 通常會向整個網路廣播更新,更新 IP 到硬體(MAC)的映射,以確保流量傳送到正確的實體或虛擬網路卡。
簡單理解
當一台電腦(例如你的筆電)想要跟同一區域網內的另一台主機通訊,它只知道對方的 IP 位址(例如 192.168.1.10),但真正發資料需要的是對方的 MAC 位址(像 00:1a:2b:3c:4d:5e)──這時就靠 ARP。
運作流程:
-
A 想發封包給 B(知道 B 的 IP)
-
A 查自己 ARP cache,沒有對應的 MAC
-
A 發出一個 ARP 請求(廣播):
「誰是 192.168.1.10?請回傳你的 MAC 給我」
-
B 收到後回傳 ARP 回應:
「我是!我的 MAC 是 00:1a:2b:3c:4d:5e」
-
A 收到後把 IP→MAC 關係暫存起來(放進 ARP 快取)
接著就能發出以對方 MAC 為目的的乙太網封包了
相關命令
在 Linux/macOS:
- 顯示目前 ARP 快取
- 查看 ARP 表(較新用法)
ARP 攻擊(ARP Spoofing)
因為 ARP 沒有驗證機制,某些駭客會假冒 IP 位址回應假的 MAC,讓資料誤傳到他那邊,這叫 中間人攻擊(Man-in-the-Middle)。
Reference
kube-vip