Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
絶対にExportされてないフィールドを書き換えるなよ!絶対だぞ!絶対! / golang.t...
Search
morikuni
December 18, 2018
Programming
270
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
絶対にExportされてないフィールドを書き換えるなよ!絶対だぞ!絶対! / golang.tokyo#20
morikuni
December 18, 2018
More Decks by morikuni
See All by morikuni
エラー設計について / Designing Errors
morikuni
7
3.2k
GopherCon 2018/2019
morikuni
0
120
Architecture & Go kit
morikuni
2
2.1k
ホリネズミでもわかるGoroutine入門 / golang.tokyo#14
morikuni
12
9.1k
DIコンテナを使わないDI / golang.tokyo#11
morikuni
4
15k
Other Decks in Programming
See All in Programming
Jetpack Compose Mechanisms
skydoves
3
880
Apple Intelligenceとオンデバイス時代の到来
yahir0
0
160
残高管理から台帳サービスへの進化
artoy
1
220
PyConTW 2026 - AI Coding 不只是工具升級, 區域醫院醫療資訊系統協作開發與資訊團隊轉型實踐
cclai999
0
200
AI主導のプログラミング学習アプリを作ってみた
econanringo
0
200
HTML-in-Canvasで作る未来のWeb UI表現
clockmaker
5
5.9k
現場と育てるRailsモデリング 〜Spreadsheetから始めたモデル設計〜
siroemk
2
1.1k
Spec-Driven Development - Why Vaadin Is the Perfect Fit
simas
PRO
0
150
クラウドエージェントは、 何がそんなに便利なのか / what makes cloud agents so useful
rkaga
3
600
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.3k
Kotlinize Is Not Just Converting to Kotlin
chigichan24
1
200
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
130
Featured
See All Featured
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.4k
Embracing the Ebb and Flow
colly
88
5.2k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
610
Believing is Seeing
oripsolob
1
250
How to make the Groovebox
asonas
2
2.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
Self-Hosted WebAssembly Runtime for Runtime-Neutral Checkpoint/Restore in Edge–Cloud Continuum
chikuwait
0
900
Crafting Experiences
bethany
1
390
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
950
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
540
Transcript
絶対にExportされてないフィールドを書き換える なよ!絶対だぞ!絶対! golang.tokyo #20
About me • morikuni • https://twitter.com/inukirom • https://github.com/morikuni • Mercari
Microservices Development • Go & Application Architecture • mercari.go に来て!
絶対にExportされてないフィールドを書き換える なよ!絶対だぞ!絶対!
「あれ?Exportされてないフィールドってそ もそも書き換えられないよね?何言ってる の?」 Goの有識者の声
書き換えに成功して しまいまいした。 大変申し訳ありませんが github.com/morikuni/go-experiment/overwrite
Usage import ( "testing" "github.com/morikuni/go-experiment/overwrite" "github.com/morikuni/go-experiment/overwrite/internal" ) func TestField(t *testing.T)
{ x := internal.NewX("aaa") overwrite.Field(&x, "val", "bbb") if got, want := x.Get(), "bbb"; got != want { t.Errorf("got %v, want %v", got, want) } } package internal type X struct { val string } func (x X) Get()string { return x.val } func NewX(a string) X{ return X{a} }
Implementation func Field(target interface{}, field string, val interface{}) error {
targetVal := reflect.Indirect(reflect.ValueOf(target)) // セットするstructのreflect.Valueをとる if targetVal.Kind() != reflect.Struct { return ErrNotStruct } dstVal := targetVal.FieldByName(field) // セットするフィールドのreflect.Valueをとる if !dstVal.IsValid() { return ErrNoSuchField } srcVal := reflect.ValueOf(val) // セットする値のreflect.Valueをとる if srcVal.Type() != dstVal.Type() { return ErrTypeMismatch } dstAddr := unsafe.Pointer(dstVal.UnsafeAddr()) // セットするフィールドのアドレスをとる setableField := reflect.NewAt(dstVal.Type(), dstAddr).Elem() // セットするフィールドのアドレスにval型の変数を作成する setableField.Set(srcVal) // setableFieldはただの変数なので値をセットできる return nil }
まとめ • Exportされていないフィールドを書き換えました • 絶対に本番環境ではつかうなよ!絶対だぞ!絶対!