Upgrade to Pro — share decks privately, control downloads, hide ads and more …

interfaceとの付き合い

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on.

 interfaceとの付き合い

Avatar for michiru shirakawa

michiru shirakawa

November 14, 2017
Tweet

More Decks by michiru shirakawa

Other Decks in Programming

Transcript

  1. WomenWhoGoTokyo αϯϑϥϯγείൃ঵ͷ, ঁੑͷGoݴޠͷίϛϡχςΟͰ͢ɻ ݱࡏ11Χࠃ(౎ࢢผͰ͸25ڌ఺ ❗ )ఔͰߦΘΕ͓ͯΓ, ౦ژ͸2016೥ 4݄ʹൃ଍͠·ͨ͠ɻ ݄ʹ1ճ, ฏ೔ͷ໷19͔࣌Β౎಺Ͱू·Γ,

    ษڧձΛߦ͍ͬͯ·͢ɻ ܦݧ΍εΩϧ͸·ͬͨ͘໰͍·ͤΜɻॳΊͯͷํ΋ࢀՃ͠΍͍͢ ձΛ໨ࢦ͍ͯ͠·͢㷉◔ϖ◔ʔ ©micchie Women Who go Tokyo, 2017 2
  2. ͜Μͳײ͡ͷJSONΛ... [ { "11": ["ΞϝϦΧάϚ", "ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ", 100] }, { "12":

    ["πΩϊϫάϚ", "टͷ෦෼͕ന͍ໟͰࡾ೔݄ঢ়ʹݟ͑Δ͜ͱ͔ΒπΩϊϫάϚͱݺ͹ΕΔɻ", 90] }, { "13": ["ϋΠΠϩάϚ", "ผ໊άϦζϦʔɻώάϚͷ஥ؒɻ", 30] }, { "14": ["ϗοΩϣΫάϚ", "γϩΫϚɻ๺ۃʹ͍Δ͕ɺೆۃʹ͸͍ͳ͍ɻ஍্࠷େͷ೑৯्ɻݮ͍ͬͯΔɻ", 80] }, { "15": ["ϚϨʔάϚ", "খ͘͞໨͕ѱ͘ɺઉ͕௕͍ɻύσΟϯτϯɻ", 50] } ] ©micchie Women Who go Tokyo, 2017 7
  3. ͜Μͳߏ଄Ͱ؅ཧͯ͠, ಉ͡Α͏ʹग़ྗ͍ͨ͠ɻ 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
  4. ૉ௚ʹ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
  5. ͦ͜Ͱ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
  6. 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
  7. 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
  8. ;Δ͍Ո͔Β͋ͨΒ͍͠ՈʹҾͬӽ͠Λ͍ͨ͠ɻ#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
  9. ಉ͡ܕಉ࢜͸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
  10. ͱ͜ΖͰ, 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
  11. • ϥϕϧ • @mi-bear, @micchiebear • Ϩϕϧ • ೴ےઓ࢜ (15.0)

    • Gopher (1.5) • Vim (0.3) • ଐੑ, ܕ • ! , " , # , 3DS • גࣜձࣾϦϒηϯε ©micchie Women Who go Tokyo, 2017 18
  12. Thanks! Love Gophers Love Gophers The Go gopher was designed

    by Renée French. ©micchie Women Who go Tokyo, 2017 19