Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
interfaceとの付き合い
Search
michiru shirakawa
November 14, 2017
Programming
2.3k
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
interfaceとの付き合い
michiru shirakawa
November 14, 2017
More Decks by michiru shirakawa
See All by michiru shirakawa
2025年予言の書
mishirakawa
1
400
あらゆる変化を受け入れながら 働きつづける - 介護・学業編
mishirakawa
3
3.8k
まだまだ楽しい!あらゆる変化を受け入れながら 働きつづけるコツ
mishirakawa
0
1.7k
技術書典のネタづくり
mishirakawa
0
570
Go の並行処理を体験してみよう
mishirakawa
1
180
VimConf の効用
mishirakawa
0
57
Lose Weight with Vim and Go
mishirakawa
0
220
はじめてのプログラミングにGo
mishirakawa
2
1.7k
Introduction of Women Who Go Tokyo
mishirakawa
2
3.2k
Other Decks in Programming
See All in Programming
Foundry Localでエージェント開発
seosoft
0
120
高専キャリア LT 発表内容
crysta1221
6
5.5k
DynamoDBの基礎を振り返りながらベクトル検索機能を理解する
musan
3
260
私のClaude Code活用法 (個人開発編) - PHPerKaigi mini #4(2026/08/24)
panda_program
1
210
初心者DevRelとして参加者だった私が、DevRel Talks!#2に登壇するまでにしてきたこと
sokohirai
0
330
30年振りにコンパイラの定数整数除算を改善した
herumi
9
4.4k
Oxlintはいいぞ(続)
yug1224
1
540
Gmail/Google DriveをトリガーにAIエージェントを動かそう! / Run AI agents with Gmail/Google Drive as triggers!
har1101
3
460
「AI時代、配布するPythonコードをどう守るか: 難読化の実験と判断軸」 #PyconJP2026
pkshadeck
PRO
2
150
What We Talk About When We Talk About XP
m_seki
2
340
AIに既存システムを理解させる技術 ~レガシーを見捨てないハーネスエンジニアリング入門~
ochtum
0
200
Discordを用いたラボオートメーション関連情報収集の自動化
noguhiro2002
0
500
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
330
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
1.2k
The Invisible Side of Design
smashingmag
301
52k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
800
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
430
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
Paper Plane (Part 1)
katiecoart
PRO
1
10k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
The Pragmatic Product Professional
lauravandoore
37
7.4k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
Transcript
golang.tokyo #10 x WomenWhoGoTokyo interfaceͱͷ͖߹͍ micchie@WomenWhoGoTokyo ©micchie Women Who go
Tokyo, 2017 1
WomenWhoGoTokyo αϯϑϥϯγείൃͷ, ঁੑͷGoݴޠͷίϛϡχςΟͰ͢ɻ ݱࡏ11Χࠃ(ࢢผͰ25ڌ ❗ )ఔͰߦΘΕ͓ͯΓ, ౦ژ2016 4݄ʹൃ͠·ͨ͠ɻ ݄ʹ1ճ, ฏͷ19͔࣌ΒͰू·Γ,
ษڧձΛߦ͍ͬͯ·͢ɻ ܦݧεΩϧ·͍ͬͨ͘·ͤΜɻॳΊͯͷํࢀՃ͍͢͠ ձΛࢦ͍ͯ͠·͢㷉◔ϖ◔ʔ ©micchie Women Who go Tokyo, 2017 2
ʕ•ᴥ•ʔʻͳ͠Λ͍ͨ͜͠ͱ interfaceʹ͍ͭͯ ͜Μͳ෩ʹΓ·ͨ͠ ©micchie Women Who go Tokyo, 2017 3
͘͢͝టष͍Ͱ͢ɻ ©micchie Women Who go Tokyo, 2017 4
ʕ•ᴥ•ʔʻࢥ͍͜Έ ͔ͬ͠Γͱͨ͠ܕ͚ݴޠͰ͋Δ͕Ώ͑ʹ ॊೈͳऔѻ͍͍͠ͷͰͳ͍͔ ©micchie Women Who go Tokyo, 2017 5
ʕ•̀ᴥ•́ʔʻࣗ༝ͳߏͷJsonϑΝΠϧΛऔΓࠐΈ͍ͨΜͰ͢ ʕ•́ᴥ•̀ʔʻݹʹઃܭ͞Εͨڥ͔Β৽͍͠ͷʹҠߦ͍ͨ͠ΜͰ͢ ©micchie Women Who go Tokyo, 2017 6
͜Μͳײ͡ͷJSONΛ... [ { "11": ["ΞϝϦΧάϚ", "ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ", 100] }, { "12":
["πΩϊϫάϚ", "टͷ෦͕ന͍ໟͰࡾ݄ঢ়ʹݟ͑Δ͜ͱ͔ΒπΩϊϫάϚͱݺΕΔɻ", 90] }, { "13": ["ϋΠΠϩάϚ", "ผ໊άϦζϦʔɻώάϚͷؒɻ", 30] }, { "14": ["ϗοΩϣΫάϚ", "γϩΫϚɻۃʹ͍Δ͕ɺೆۃʹ͍ͳ͍ɻ্࠷େͷ৯्ɻݮ͍ͬͯΔɻ", 80] }, { "15": ["ϚϨʔάϚ", "খ͕͘͞ѱ͘ɺઉ͕͍ɻύσΟϯτϯɻ", 50] } ] ©micchie Women Who go Tokyo, 2017 7
͜ΜͳߏͰཧͯ͠, ಉ͡Α͏ʹग़ྗ͍ͨ͠ɻ DataBase: CREATE TABLE `animals` ( `id` bigint NOT
NULL PRIMARY KEY, `name` varchar(255) NOT NULL, `description` text NOT NULL, `rate` int NOT NULL ) Struct: type Animals struct { ID uint `json:"id"` Name string `json:"name"` Description string `json:"description"` Rate int `json:"rate"` } ©micchie Women Who go Tokyo, 2017 8
ૉʹAnimalsߏମΛjson.Marshal͢Δͱ, ԼهͷΑ͏ʹͳΔɻ [ { "id": 11, "name": "ΞϝϦΧάϚ", "description": "ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ",
"rate": 100 }, { "id": 12, "name": "πΩϊϫάϚ", "description": "टͷ෦͕ന͍ໟͰࡾ݄ঢ়ʹݟ͑Δ͜ͱ͔ΒπΩϊϫάϚͱݺΕΔɻ", "rate": 90 }, ... ... { "id": 15, "name": "ϚϨʔάϚ", "description": "খ͕͘͞ѱ͘ɺઉ͕͍ɻύσΟϯτϯɻ", "rate": 50 } ] ©micchie Women Who go Tokyo, 2017 9
ͦ͜ͰinterfaceͰΰχϣΰχϣ͢Δͱ... ... var list []interface{} var row Animals for rows.Next()
{ ... ... id := fmt.Sprint(row.ID) // IDΛstringʹ descriptions := map[string]interface{}{id: []interface{}{row.Name, row.Description, row.Rate}} list = append(list, descriptions) } ... j, err := json.Marshal(list) ... ©micchie Women Who go Tokyo, 2017 10
json.Unmarshal͢Δͱ... &[ map[11:[ΞϝϦΧάϚ ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ 100]] map[12:[πΩϊϫάϚ टͷ෦͕ന͍ໟͰࡾ݄ঢ়ʹݟ͑Δ͜ͱ͔ΒπΩϊϫάϚͱݺΕΔɻ 90]] map[13:[ϋΠΠϩάϚ ผ໊άϦζϦʔɻώάϚͷؒɻ
30]] map[14:[ϗοΩϣΫάϚ γϩΫϚɻۃʹ͍Δ͕ɺೆۃʹ͍ͳ͍ɻ্࠷େͷ৯्ɻݮ͍ͬͯΔɻ 80]] map[15:[ϚϨʔάϚ খ͕͘͞ѱ͘ɺઉ͕͍ɻύσΟϯτϯɻ 50]] ] ͜ͷΑ͏ʹ֨ೲ͞ΕΔͷͰ, 1ߦͣͭॲཧΛ͍ͯ͘͠ɻ ©micchie Women Who go Tokyo, 2017 11
for _, row := range list { var animal Animals
for k, r := range row.(map[string]interface{}) { i, _ := strconv.Atoi(k) // keyJSON্stringͷͨΊ animal.ID = uint(i) v := r.([]interface{}) // fmt.Println(v) // [ΞϝϦΧάϚ ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ 100] animal.Name = v[0].(string) animal.Description = v[1].(string) animal.Rate = int(v[2].(float64)) } ... // insertॲཧ } ©micchie Women Who go Tokyo, 2017 12
ʕ•̀ᴥ•́ʔʻࣗ༝ͳߏͷJsonϑΝΠϧΛऔΓࠐΈ͍ͨΜͰ͢ ʕ•́ᴥ•̀ʔʻݹʹઃܭ͞Εͨڥ͔Β৽͍͠ͷʹҠߦ͍ͨ͠ΜͰ͢ ©micchie Women Who go Tokyo, 2017 13
;Δ͍Ո͔Β͋ͨΒ͍͠ՈʹҾͬӽ͠Λ͍ͨ͠ɻ#1 type ( Before struct { ID uint `db:"id"` PostalCode
sql.NullInt64 `db:"postal_code"` Prefecture string `db:"prefecture"` Address sql.NullString `db:"address"` Hoge sql.NullString `db:"hoge"` } After struct { ID uint `db:"id"` PostalCode sql.NullInt64 `db:"postal_code"` Prefecture string `db:"prefecture"` Address sql.NullString `db:"address"` Hoge string `db:"hoge"` } ) #1 1ςʔϒϧͷΈൈਮ, HogeͷΈ͕ܕ͕ҟͳΔέʔεɻ ©micchie Women Who go Tokyo, 2017 14
ಉ͡ܕಉ࢜interfaceͰѻ͑·ͨ͠ɻ func main() { ... to := &After{ PostalCode: toYurufuwa(Before.PostalCode).(sql.NullInt64)
Prefecture: toYurufuwa(Before.Prefecture).(string) Address: toYurufuwa(Before.Address).()sql.NullString Hoge: toHoge(Before.Hoge) } ... } func toYurufuwa(before interface{}) interface{} { return before } func toHoge(before sql.NullString) string { if !before.Valid { return HOGE_DEFAULT } returne before.String } ©micchie Women Who go Tokyo, 2017 15
ͱ͜ΖͰ, Nullͳ߲Λѻ͏ͱ͖Կ͕࠷దͳΜͰ͠ΐ͏͔...? 1. *string *int 2. sql.NullString sql.NullInt64
3. `ͦͦNullڐՄͳઃܭΛ͠ͳ͍ ©micchie Women Who go Tokyo, 2017 16
ͱ͜ΖͰ, database/sqlͷcolumnsͲ͏ॻ͍͍ͯ·͔͢...? func (b Before) TableName() string { return "before_table_name"
} func (b Before) columns() (s []string) { t := reflect.TypeOf(c) for i := 0; i < t.NumField(); i++ { field := t.Field(i) s = append(s, field.Tag.Get("db")) } return s } ... sql := fmt.Sprintf( "SELECT %s FROM %s", strings.Join(Before.columns(), ","), Before.TableName(), ) ... ©micchie Women Who go Tokyo, 2017 17
• ϥϕϧ • @mi-bear, @micchiebear • Ϩϕϧ • ےઓ࢜ (15.0)
• Gopher (1.5) • Vim (0.3) • ଐੑ, ܕ • ! , " , # , 3DS • גࣜձࣾϦϒηϯε ©micchie Women Who go Tokyo, 2017 18
Thanks! Love Gophers Love Gophers The Go gopher was designed
by Renée French. ©micchie Women Who go Tokyo, 2017 19