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
380
あらゆる変化を受け入れながら 働きつづける - 介護・学業編
mishirakawa
3
3.8k
まだまだ楽しい!あらゆる変化を受け入れながら 働きつづけるコツ
mishirakawa
0
1.7k
技術書典のネタづくり
mishirakawa
0
570
Go の並行処理を体験してみよう
mishirakawa
1
180
VimConf の効用
mishirakawa
0
55
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
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4.9k
エンジニアと一緒にテストコードの設計と実装を改善した話
mototakatsu
0
250
スマートグラスで並列バイブコーディング
hyshu
0
280
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
180
TypeScript+Orvalで実現する型安全かつ堅牢でスケーラブルなマルチチャネル通知基盤 / TSKaigi Night talks ~after conference~
d0riven
0
390
symfony/aiとlaravel/boost
77web
0
120
act1-costs.pdf
sumedhbala
0
180
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.7k
SLOをサービス品質の共通言語にするために 取り組んできたこと
wakana0222
0
400
Contextとはなにか
chiroruxx
1
390
ランチタイムLT会3周年!ランチタイムLT会を3年間続けられたお話
y0hgi
1
130
Vite+ Unified Toolchain for the Web
naokihaba
0
710
Featured
See All Featured
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
Raft: Consensus for Rubyists
vanstee
141
7.6k
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
63
55k
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
500
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.4k
Ethics towards AI in product and experience design
skipperchong
2
320
My Coaching Mixtape
mlcsv
0
170
Navigating Team Friction
lara
192
16k
Jess Joyce - The Pitfalls of Following Frameworks
techseoconnect
PRO
1
180
From π to Pie charts
rasagy
0
230
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
350
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