Docker command snippet
老是失憶……
1 | # 跑起一個 ubuntu container 並且用 foreground 模式進入 bash |
Readmoo API Go Package
PhpStorm 快捷鍵
我的 keymap 是 Sublime + Jetbrains 部份按鍵 + 自己設再配 vim 的大雜燴
ctrl + shift + p
:執行動作(action)ctrl + p
:找檔案ctrl + r
:檔案中找 symbolctrl + alt + shift + t
:refactor 選單alt + enter
:各種神奇功能(?)alt + insert
:加入各種 codeshift + f6
:renamealt + 1
:project browse windowalt + 3
:find windowalt + 4
:run windowalt + 9
:git window
TBC…
《跑者都該懂的跑步關鍵數據》跑步技術
Go Module
Go 從 1.13 開始支援 Go Module,可以在 GOPATH
以外的地方建立 go project 並進行套件管理。一直覺得 source code 只能放在 GOPATH
裡超阿雜…
建立 project
在 GOPATH
以外的地方建立一個 directory,並且在其中執行 go mod init
:
1 | $ mkdir project |
會產生 go.mod
檔案,它會記錄 Go module 與使用的 Go 版本:
1 | module github.com/cjwind/project |
接下來在這個 directory 裡進行開發跟 build 就都一樣,重點是現在 source code 不用非得放在 GOPATH
裡啦~
套件管理
用 go get
安裝 package 後,會發現在 go.mod
多了一行 require [package] [version]
,就表示目前使用的 package 及其 version。
另外可能會出現 require [package] [version] // indirect
,這表示是我們使用的 package 所需要的 package。
也可以用 go get [package]@[version]
來指定特定的 package version。
Ref
Dockerfile
COPY
COPY
如果 source 是 directory,會 copy directory 的內容,但是 directory 本身不會 copy。
假設有個資料夾叫 css/
,底下有兩個 file foo.css
跟 bar.css
。
1 | COPY ./css /workspace/ |
這樣在 container 裡會變成 /workspace/
底下有 foo.css
跟 bar.css
,而不是 /workspace/css/
底下有 foo.css
跟 bar.css
。想要是 /workspace/css/
底下有兩個 file 得這樣寫:
1 | COPY ./css /workspace/css |
Go json and embedded struct
使用 embedded struct 做 json 的 marshal 跟 unmarshal 時,json 欄位會省略 struct embedded 欄位的中間名,以比較簡潔的形式呈現。如果 struct 有寫出欄位名稱,json 欄位就會多那一層。
使用 embedded struct
1 | type Serving struct { |
marshal 結果:
1 | { |
不使用 embedded struct
1 | type Food struct { |
marshal 結果:
1 | { |
不用 embeded struct 就會有一層 Serving
,用 embedded struct 就會省略 Serving
這層。
Git submodule
submodule 是在 git repos 中使用別的 repos 的方式之一。
git 的 submodule 是記錄一個指到別人 repo 某個 commit 的指標。對主 repo 來說,記錄的只是一個 submodule commit hash。
切到 submodule 的目錄時做 git 操作會是在操作另一個 repo。
加入 submodule
1 | $ git submodule add <repo path> |
clone 含有 submodule 的 repos
clone 含有 submodule 的 repos 後,submodule 的目錄會是空的,要做以下動作來初始化:
1 | $ git submodule init |
git submodule update
會讓 submodule 的內容回到記錄的 commit。
更新 submodule
submodule 的 repo 更新或者想用不同版本(commit)的 submodule 時,要做以下操作:
1 | $ cd submodule_dir |
概念是把 submodule 的 repo 更新或者 checkout 到想要的 commit,再在主 repo 更新記錄的 submodule commit hash。
移除 submodule
1 | $ git rm -rf submodule_dir |
.gitmodules
檔案 .gitmodules
會記錄有哪些 submodule。
Go init() function
任何檔案可以擁有任意數量的 init()
function:
1 | func init() { |
init()
會在程式啟動時自動以宣告的順序執行,但不能被 call 或參考。
假設有以下兩個 go 檔案:
1 | package main |
1 | package main |
在 go run
以不同的順序指定 source file 會有不同結果:
1 | $ go run foo.go main.go |
不指定 file 的話 go
會將 file 以其名稱排序。
如果嘗試直接 call init()
則會 compile error:
1 | package main |