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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
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.7k
まだまだ楽しい!あらゆる変化を受け入れながら 働きつづけるコツ
mishirakawa
0
1.7k
技術書典のネタづくり
mishirakawa
0
570
Go の並行処理を体験してみよう
mishirakawa
1
170
VimConf の効用
mishirakawa
0
52
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
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
メソッドのジェネリクスでGoの夢は広がるか? / Kyoto.go #65
utgwkk
3
770
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
250
さぁV100、メモリをお食べ・・・
nilpe
0
140
スマートグラスで並列バイブコーディング
hyshu
0
140
AIだと陥りがちなJakarta EE最新技術への移行時の落とし穴と解決策
tnagao7
0
110
Vite+ Unified Toolchain for the Web
naokihaba
0
310
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
240
タクシーアプリ『GO』の バックエンド開発のおける AI利活用と若者のすべて
pyama86
3
2k
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
690
Inside Stream API
skrb
1
720
LLM本来の能力を解き放つサンドボックス技術とAI民主化への適用
yukukotani
3
4k
Featured
See All Featured
From π to Pie charts
rasagy
0
210
Lightning talk: Run Django tests with GitHub Actions
sabderemane
0
200
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
140
Faster Mobile Websites
deanohume
310
31k
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
180
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
210
Automating Front-end Workflow
addyosmani
1370
210k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
35
2.5k
How to Get Subject Matter Experts Bought In and Actively Contributing to SEO & PR Initiatives.
livdayseo
0
140
For a Future-Friendly Web
brad_frost
183
10k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
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