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
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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
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
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
450
速く作れる。その次は、速く確かめられる開発へ 〜AIネイティブ開発を支える、Shift Down〜 / Can build fast. Next, moving to development where we can verify fast.
rkaga
8
5.5k
パーソナライズPodcastを作った話
repunit11
0
120
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
330
iOS 27でニュースアプリはどう変わる!? 〜日経電子版の新機能対応と、開発事例から〜
lynnswap
7
13k
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
580
速習iPhone Duo対応
yuukiw00w
2
960
Workers Cache を知る
syumai
0
340
wkhtmltopdfの次どうするか問題2026
willnet
2
1.9k
すこし踏み込む CancellationToken
htkym
2
1.6k
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.1k
Featured
See All Featured
Scaling GitHub
holman
464
140k
Ethics towards AI in product and experience design
skipperchong
2
410
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
620
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
128
56k
Bash Introduction
62gerente
615
220k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
550
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
250
Utilizing Notion as your number one productivity tool
mfonobong
4
610
Chasing Engaging Ingredients in Design
codingconduct
0
340
Making the Leap to Tech Lead
cromwellryan
135
10k
Designing for Performance
lara
611
70k
So, you think you're a good person
axbom
PRO
2
2.2k
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されていないフィールドを書き換えました • 絶対に本番環境ではつかうなよ!絶対だぞ!絶対!