Slide 1

Slide 1 text

golang.tokyo #10 x WomenWhoGoTokyo interfaceͱͷ෇͖߹͍ micchie@WomenWhoGoTokyo ©micchie Women Who go Tokyo, 2017 1

Slide 2

Slide 2 text

WomenWhoGoTokyo αϯϑϥϯγείൃ঵ͷ, ঁੑͷGoݴޠͷίϛϡχςΟͰ͢ɻ ݱࡏ11Χࠃ(౎ࢢผͰ͸25ڌ఺ ❗ )ఔͰߦΘΕ͓ͯΓ, ౦ژ͸2016೥ 4݄ʹൃ଍͠·ͨ͠ɻ ݄ʹ1ճ, ฏ೔ͷ໷19͔࣌Β౎಺Ͱू·Γ, ษڧձΛߦ͍ͬͯ·͢ɻ ܦݧ΍εΩϧ͸·ͬͨ͘໰͍·ͤΜɻॳΊͯͷํ΋ࢀՃ͠΍͍͢ ձΛ໨ࢦ͍ͯ͠·͢㷉◔ϖ◔ʔ ©micchie Women Who go Tokyo, 2017 2

Slide 3

Slide 3 text

ʕ•ᴥ•ʔʻ͸ͳ͠Λ͍ͨ͜͠ͱ interfaceʹ͍ͭͯ ͜Μͳ෩ʹ஌Γ·ͨ͠ ©micchie Women Who go Tokyo, 2017 3

Slide 4

Slide 4 text

͘͢͝టष͍Ͱ͢ɻ ©micchie Women Who go Tokyo, 2017 4

Slide 5

Slide 5 text

ʕ•ᴥ•ʔʻࢥ͍͜Έ ͔ͬ͠Γͱͨ͠ܕ෇͚ݴޠͰ͋Δ͕Ώ͑ʹ ॊೈͳऔѻ͍͸೉͍͠ͷͰ͸ͳ͍͔ ©micchie Women Who go Tokyo, 2017 5

Slide 6

Slide 6 text

ʕ•̀ᴥ•́ʔʻࣗ༝ͳߏ଄ͷJsonϑΝΠϧΛऔΓࠐΈ͍ͨΜͰ͢ ʕ•́ᴥ•̀ʔʻݹʹઃܭ͞Εͨ؀ڥ͔Β৽͍͠ͷʹҠߦ͍ͨ͠ΜͰ͢ ©micchie Women Who go Tokyo, 2017 6

Slide 7

Slide 7 text

͜Μͳײ͡ͷJSONΛ... [ { "11": ["ΞϝϦΧάϚ", "ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ", 100] }, { "12": ["πΩϊϫάϚ", "टͷ෦෼͕ന͍ໟͰࡾ೔݄ঢ়ʹݟ͑Δ͜ͱ͔ΒπΩϊϫάϚͱݺ͹ΕΔɻ", 90] }, { "13": ["ϋΠΠϩάϚ", "ผ໊άϦζϦʔɻώάϚͷ஥ؒɻ", 30] }, { "14": ["ϗοΩϣΫάϚ", "γϩΫϚɻ๺ۃʹ͍Δ͕ɺೆۃʹ͸͍ͳ͍ɻ஍্࠷େͷ೑৯्ɻݮ͍ͬͯΔɻ", 80] }, { "15": ["ϚϨʔάϚ", "খ͘͞໨͕ѱ͘ɺઉ͕௕͍ɻύσΟϯτϯɻ", 50] } ] ©micchie Women Who go Tokyo, 2017 7

Slide 8

Slide 8 text

͜Μͳߏ଄Ͱ؅ཧͯ͠, ಉ͡Α͏ʹग़ྗ͍ͨ͠ɻ 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

Slide 9

Slide 9 text

ૉ௚ʹ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

Slide 10

Slide 10 text

ͦ͜Ͱ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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

for _, row := range list { var animal Animals for k, r := range row.(map[string]interface{}) { i, _ := strconv.Atoi(k) // key͸JSON্͸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

Slide 13

Slide 13 text

ʕ•̀ᴥ•́ʔʻࣗ༝ͳߏ଄ͷJsonϑΝΠϧΛऔΓࠐΈ͍ͨΜͰ͢ ʕ•́ᴥ•̀ʔʻݹʹઃܭ͞Εͨ؀ڥ͔Β৽͍͠ͷʹҠߦ͍ͨ͠ΜͰ͢ ©micchie Women Who go Tokyo, 2017 13

Slide 14

Slide 14 text

;Δ͍Ո͔Β͋ͨΒ͍͠ՈʹҾͬӽ͠Λ͍ͨ͠ɻ#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

Slide 15

Slide 15 text

ಉ͡ܕಉ࢜͸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

Slide 16

Slide 16 text

ͱ͜ΖͰ, Nullͳ߲໨Λѻ͏ͱ͖͸Կ͕࠷దͳΜͰ͠ΐ͏͔...? 1. *string ΍ *int 2. sql.NullString ΍ sql.NullInt64 3. `ͦ΋ͦ΋NullڐՄͳઃܭΛ͠ͳ͍ ©micchie Women Who go Tokyo, 2017 16

Slide 17

Slide 17 text

ͱ͜ΖͰ, 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

Slide 18

Slide 18 text

• ϥϕϧ • @mi-bear, @micchiebear • Ϩϕϧ • ೴ےઓ࢜ (15.0) • Gopher (1.5) • Vim (0.3) • ଐੑ, ܕ • ! , " , # , 3DS • גࣜձࣾϦϒηϯε ©micchie Women Who go Tokyo, 2017 18

Slide 19

Slide 19 text

Thanks! Love Gophers Love Gophers The Go gopher was designed by Renée French. ©micchie Women Who go Tokyo, 2017 19