interfaceとの付き合い

 interfaceとの付き合い

112f4b6a485e501d03c8940d2a764c2b?s=128

michiru shirakawa

November 14, 2017
Tweet

Transcript

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

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

    ษڧձΛߦ͍ͬͯ·͢ɻ ܦݧ΍εΩϧ͸·ͬͨ͘໰͍·ͤΜɻॳΊͯͷํ΋ࢀՃ͠΍͍͢ ձΛ໨ࢦ͍ͯ͠·͢㷉◔ϖ◔ʔ ©micchie Women Who go Tokyo, 2017 2
  3. ʕ•ᴥ•ʔʻ͸ͳ͠Λ͍ͨ͜͠ͱ interfaceʹ͍ͭͯ ͜Μͳ෩ʹ஌Γ·ͨ͠ ©micchie Women Who go Tokyo, 2017 3

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

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

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

  7. ͜Μͳײ͡ͷJSONΛ... [ { "11": ["ΞϝϦΧάϚ", "ผ໊ΞϝϦΧΫϩΫϚɻϓʔ͞ΜͷϞσϧɻ", 100] }, { "12":

    ["πΩϊϫάϚ", "टͷ෦෼͕ന͍ໟͰࡾ೔݄ঢ়ʹݟ͑Δ͜ͱ͔ΒπΩϊϫάϚͱݺ͹ΕΔɻ", 90] }, { "13": ["ϋΠΠϩάϚ", "ผ໊άϦζϦʔɻώάϚͷ஥ؒɻ", 30] }, { "14": ["ϗοΩϣΫάϚ", "γϩΫϚɻ๺ۃʹ͍Δ͕ɺೆۃʹ͸͍ͳ͍ɻ஍্࠷େͷ೑৯्ɻݮ͍ͬͯΔɻ", 80] }, { "15": ["ϚϨʔάϚ", "খ͘͞໨͕ѱ͘ɺઉ͕௕͍ɻύσΟϯτϯɻ", 50] } ] ©micchie Women Who go Tokyo, 2017 7
  8. ͜Μͳߏ଄Ͱ؅ཧͯ͠, ಉ͡Α͏ʹग़ྗ͍ͨ͠ɻ 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
  9. ૉ௚ʹ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
  10. ͦ͜Ͱ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
  11. 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
  12. 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
  13. ʕ•̀ᴥ•́ʔʻࣗ༝ͳߏ଄ͷJsonϑΝΠϧΛऔΓࠐΈ͍ͨΜͰ͢ ʕ•́ᴥ•̀ʔʻݹʹઃܭ͞Εͨ؀ڥ͔Β৽͍͠ͷʹҠߦ͍ͨ͠ΜͰ͢ ©micchie Women Who go Tokyo, 2017 13

  14. ;Δ͍Ո͔Β͋ͨΒ͍͠ՈʹҾͬӽ͠Λ͍ͨ͠ɻ#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
  15. ಉ͡ܕಉ࢜͸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
  16. ͱ͜ΖͰ, Nullͳ߲໨Λѻ͏ͱ͖͸Կ͕࠷దͳΜͰ͠ΐ͏͔...? 1. *string ΍ *int 2. sql.NullString ΍ sql.NullInt64

    3. `ͦ΋ͦ΋NullڐՄͳઃܭΛ͠ͳ͍ ©micchie Women Who go Tokyo, 2017 16
  17. ͱ͜ΖͰ, 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
  18. • ϥϕϧ • @mi-bear, @micchiebear • Ϩϕϧ • ೴ےઓ࢜ (15.0)

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

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