or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
議程3 - Reflect 用太多,都不好意思跟別人說 Go 靜態了 - 高宜誠 / YC Kao
tags:
GopherDay2024
Agenda
Slido 連結
投影片連結
Why Reflect?
主要為動態語言與靜態語言差異:
天有不測風雲,人有 JSON YAML – YC. 2024
What is reflect?
靜態的處理動態內容
可以做到
How reflect works
ABI (Application Binary Interface)
紀錄每個 type 的 memory layout
reflect rely on ABI
reflect.Type
紀錄該型別的描述,例如是否 comparable
從 abi 透過 unsafe.Pinter() 硬轉 rtype(具有所有資訊),並再轉 reflect.Type(具有 exposed 資訊)
例如是指標 type,可以拿到他指向的 type
StructField: 代表一個 struct 裡的一個 field,包含 field name, type, tag 等資訊
Anonymous
代表是否為 embedded fieldfun fact: 可以將
type C struct
放進其他 struct{A C} 並修改Anonymous
,達到名稱與型別不同的 embedded field用 StructOf 可以拿多個 StructField,轉換成一個 type
reflect.Value
沒有 expose 任何 field,所有操作都要透過 reflect package,例如reflect.Value.CanInt()
小技巧
v.FieldByName("Known").Interface().(Known)
可以快速拿到已知 struct 的 field value (差異五倍)// 求 source code
https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/reflect/value.go;l=310
Q&A
reflect 從 abi 拿完整 memory layout 等等,也會被 GC 正常管理記憶體,相較直接使用 unsafe 來的安全(?)