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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
morikuni
December 18, 2018
Programming
260
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
110
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
From 6 People Classroom Meetup to 100 People Regional Conference / FOSS4G Hiroshima 2026
furukawayasuto
0
170
一参加者から『中の人』へ 〜全通PHPerがブースに立って学んだ、カンファレンスを100倍楽しむコツ〜
wp_daisuke
0
150
Deep dive into the select statement (GopherCon UK)
jespino
0
180
選挙速報を多くのユーザーへ 届ける Live Activities 設計
hamayokokuririn
0
130
業務時間外もAIに働いてもらう話
colorful12
3
10k
Swift愛好会100回記念 第1回を振り返る
jollyjoester
0
120
【高い買い物LT会】初任給で話題の国産フィジカルAIを買った話
akagami
PRO
0
150
The Good Stuff, Not the Slop: Engineering High-Quality Android Apps with Modern AI Tooling
danybony
1
250
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
180
変化を抱擁するドキュメントの作り方 - ビジネスルール駆動開発がもたらす、コードとの新しい関係
ioki
2
170
Omarchy Tokyo やると聞いて UMPC 買ってセットアップしてきた
mtsmfm
0
140
iOS開発×AI駆動開発 〜最近使って便利だったスキルの話〜
nogu66
0
150
Featured
See All Featured
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
287
14k
From π to Pie charts
rasagy
0
360
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
490
Optimizing for Happiness
mojombo
378
71k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
600
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
Site-Speed That Sticks
csswizardry
13
1.5k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
390
Color Theory Basics | Prateek | Gurzu
gurzu
0
460
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Mozcon NYC 2025: Stop Losing SEO Traffic
samtorres
1
530
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
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されていないフィールドを書き換えました • 絶対に本番環境ではつかうなよ!絶対だぞ!絶対!