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
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
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
51
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
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
250
AIエージェントと協働するCLI開発 — BunとOpenClawで学んだこと
yoshikouki
1
240
DynamoDBには集計系のクエリがないけどなんとかしたい
musan
1
130
Inside Stream API
skrb
1
640
エージェンティックRAGにAWSで入門しよう!
har1101
8
1.2k
ユニットテストの先へ:テスト技法で要求・仕様を整理するJava開発実践 / Beyond_Unit_Testing_Practical_Java_Development_Techniques_for_Organizing_Requirements_and_Specifications
shimashima35
0
350
フロントエンドとバックエンドで「1文字」を揃えよう
youkidearitai
PRO
0
210
AIとRubyの静的型付け
ukin0k0
0
540
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
500
Language Server 使ってる? 〜VSCode と Zed の場合〜 / Are you using a Language Server? ~For VS Code and Zed~
handlename
0
760
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.1k
dRuby over BLE
makicamel
2
320
Featured
See All Featured
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Abbi's Birthday
coloredviolet
2
7.9k
Technical Leadership for Architectural Decision Making
baasie
3
400
Balancing Empowerment & Direction
lara
6
1.1k
Facilitating Awesome Meetings
lara
57
6.9k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
2k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
27k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.7k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.5k
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
520
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