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
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
半永久的に提供し続けられるプライベートクラウドを目指して ― 利用者の認知負荷を抑えるAPI抽象化とハードウェア世代交代の基盤設計
tomokon
0
430
【加筆修正版】Laravel のアプリケーションをどこにデプロイするか #phpcon_ehime
akase244
0
140
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
110
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
150
Verilogで学ぶCPU自作入門.pdf
uyuki234
8
4k
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
510
カツオ、ご期待ください
suneo3476
0
140
GitHubハンズオン講座 — 実務レベルのチーム開発のフローを身につけよう
junhat6
0
120
モバイル交通系ICへのチャージ実例から考える、クロスプラットフォーム開発におけるiOS実機テスト設計とCI運用
yusuga
1
580
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
340
SREの越境 / SRE Collaboration
y0hgi
2
300
MVNOの申込からeSIM開通までをiOSアプリでつなぐ- 本人確認・MNP・通信事業者基盤をまたぐ実装
satotakeshi
0
540
Featured
See All Featured
Tell your own story through comics
letsgokoyo
1
1.1k
The innovator’s Mindset - Leading Through an Era of Exponential Change - McGill University 2025
jdejongh
PRO
1
350
Automating Front-end Workflow
addyosmani
1369
210k
Abbi's Birthday
coloredviolet
4
10k
Navigating Weather and Climate Data
rabernat
0
550
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
350
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Effective software design: The role of men in debugging patriarchy in IT @ Voxxed Days AMS
baasie
1
550
How to Ace a Technical Interview
jacobian
280
24k
WENDY [Excerpt]
tessaabrams
14
40k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
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されていないフィールドを書き換えました • 絶対に本番環境ではつかうなよ!絶対だぞ!絶対!