{"id":2775,"date":"2022-07-21T12:40:14","date_gmt":"2022-07-21T03:40:14","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/?p=2775"},"modified":"2022-07-21T12:40:14","modified_gmt":"2022-07-21T03:40:14","slug":"golang-tag","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2022\/07\/21\/golang-tag\/","title":{"rendered":"Go\u3067\u69cb\u9020\u4f53\u306e\u30bf\u30b0\u3092\u6d3b\u7528\u3057\u3066Excel\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080"},"content":{"rendered":"
\u3053\u3093\u306b\u3061\u306f\u3002\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u7267\u7530\u3067\u3059\u3002
\n\u4ee5\u524d\u5b9f\u88c5\u3057\u305f\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u306e\u6a5f\u80fd\u3068\u3057\u3066\u3001Excel\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u6a5f\u80fd\u304c\u3042\u3063\u305f\u306e\u3067\u3059\u304c\u3001\u305d\u306e\u6642\u306b\u884c\u3063\u305f\u624b\u6cd5\u3092\u7d39\u4ecb\u3057\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002\uff08\u5b9f\u88c5\u306fGo\u8a00\u8a9e(v1.18)\u3067\u3059\uff09<\/p>\n
\u4eca\u56de\u7528\u3044\u3066\u3044\u308b\u30b5\u30f3\u30d7\u30eb\u30b3\u30fc\u30c9\u306f\u2193\u306b\u3042\u308a\u307e\u3059\u3002 \u4f8b\u3068\u3057\u3066\u4ee5\u4e0b\u306e\u3088\u3046\u306aExcel\u306e\u60c5\u5831\u3092\u8aad\u307f\u8fbc\u3080\u3053\u3068\u3092\u8003\u3048\u307e\u3059\u3002<\/p>\n <\/p>\n \u3053\u306e\u5404\u4eba\u306e\u60c5\u5831\u3092\u8aad\u307f\u53d6\u308b\u305f\u3081\u306bPerson\u3068\u3044\u3046\u69cb\u9020\u4f53\u3092\u4f5c\u308a\u307e\u3059\u3002<\/p>\n Excel\u306e\u8aad\u307f\u8fbc\u307f\u306f\u3001excelize<\/a>\u3068\u3044\u3046\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u3063\u3066\u884c\u3046\u3053\u3068\u306b\u3057\u307e\u3059\u3002 \u3042\u3068\u306f\u3001rows\u304b\u3089\u5404\u30bb\u30eb\u306e\u5024\u3092\u53d6\u5f97\u3059\u308c\u3070\u3044\u3044\u308f\u3051\u3067\u3059\u304c\u3001Excel\u306e\u5217\u540d\uff08A,B,C\u2026\uff09\u304b\u3089\u5024\u3092\u53d6\u5f97\u3059\u308b\u305f\u3081\u306b\u3001getCol\u3068\u3044\u3046\u95a2\u6570\u3092\u4f5c\u308a\u307e\u3057\u305f\u3002\uff08excelize.ColumnNameToNumber\u306f\u3001\u5217\u540d\u3092\u914d\u5217\u306e\u30a4\u30f3\u30c7\u30c3\u30af\u30b9\u306b\u5909\u63db\u3059\u308b\u95a2\u6570\u3067\u3059\u3002\uff09<\/p>\n \u3053\u308c\u3092\u4f7f\u3046\u3068\u3001\u5404\u884c\uff08\u30e9\u30d9\u30eb\u3092\u9664\u304f2\u884c\u76ee\u4ee5\u964d\uff09\u306b\u5bfe\u3057\u3066\u69cb\u9020\u4f53\u306b\u5024\u3092\u30bb\u30c3\u30c8\u3059\u308b\u30b3\u30fc\u30c9\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308b\u3068\u601d\u3044\u307e\u3059\u3002\uff08\u30a8\u30e9\u30fc\u51e6\u7406\u306f\u7701\u7565\uff09<\/p>\n \u3057\u304b\u3057\u3001\u3053\u306e\u5b9f\u88c5\u306b\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u554f\u984c\u304c\u3042\u308a\u307e\u3059\u3002<\/p>\n \u305d\u3053\u3067\u3001\u69cb\u9020\u4f53\u306e\u30bf\u30b0\u3092\u4f7f\u3046\u3068\u3001\u3053\u306e\u51e6\u7406\u3092\u3088\u308a\u7c21\u6f54\u306b\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n \u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u30e1\u30bf\u60c5\u5831\u3092\u4e0e\u3048\u308b\u305f\u3081\u306b\u3001\u30bf\u30b0\u3092\u4f7f\u3046\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 json\u30bf\u30b0\u306f\u3001\u4f8b\u3048\u3070json.Marshal\u3067\u69cb\u9020\u4f53\u3092JSON\u306b\u5909\u63db\u3059\u308b\u3068\u304d\u306bJSON\u306e\u30ad\u30fc\u540d\u3092\u6307\u5b9a\u3059\u308b\u3001\u3068\u3044\u3046\u3088\u3046\u306b\u4f7f\u308f\u308c\u307e\u3059\u3002\u4eca\u56de\u306f\u3001Excel\u306e\u5217\u540d\u3092\u8868\u3059column\u30bf\u30b0\u3092\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n \u4ed8\u4e0e\u3057\u305f\u30bf\u30b0\u306e\u60c5\u5831\u306freflect\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u3066\u53d6\u5f97\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002 reflect\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u3001\u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u6570\u3060\u3051\u30eb\u30fc\u30d7\u3092\u56de\u3057\u3001\u5404\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5bfe\u3057\u3066<\/p>\n \u3068\u3044\u3046\u64cd\u4f5c\u3092\u884c\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n \u3053\u306ereflect\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f7f\u3063\u305f\u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u3054\u3068\u306e\u51e6\u7406\u306f\u3001\u5b9f\u306fjson.Unmarshal\u306e\u5185\u90e8\u3067\u3084\u3063\u3066\u3044\u308b\u3053\u3068\u3068\u4f3c\u3066\u3044\u307e\u3059\uff08json.Unmarshal\u306f\u3082\u3063\u3068\u8907\u96d1\u3067\u3059\u304c\uff09\u3002 \u307e\u305f\u3001\u518d\u5e30\u7684\u306a\u51e6\u7406\u3092\u3059\u308b\u305f\u3081\u306b\u3001\u95a2\u6570\u5185\u3067\u65b0\u305f\u306b\u95a2\u6570f\u3092\u5b9a\u7fa9\u3057\u3066\u3044\u307e\u3059\u3002\u3082\u3057\u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u304c\u3001\u57cb\u3081\u8fbc\u307e\u308c\u305f\u69cb\u9020\u4f53\u3060\u3063\u305f\u5834\u5408\u306f\u3001\u518d\u5e30\u7684\u306bf\u3092\u5b9f\u884c\u3057\u307e\u3059\u3002\u3053\u308c\u306b\u3088\u3063\u3066\u3001\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u69cb\u9020\u4f53\u3082\u9069\u5207\u306b\u5024\u3092\u8aad\u307f\u53d6\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n main\u95a2\u6570\u3068\u5b9f\u884c\u7d50\u679c\u306f\u4ee5\u4e0b\u306e\u901a\u308a\u3067\u3059\u3002<\/p>\n \u4eca\u56de\u306f\u3001Go\u3067\u69cb\u9020\u4f53\u306e\u30bf\u30b0\u3092\u4f7f\u7528\u3057\u3066\u3001Excel\u306e\u30c7\u30fc\u30bf\u3092\u8aad\u307f\u8fbc\u3080\u65b9\u6cd5\u3092\u7d39\u4ecb\u3057\u307e\u3057\u305f\u3002\u69cb\u9020\u4f53\u306e\u30bf\u30b0\u306f\u3001\u666e\u6bb5\u306eGo\u30d7\u30ed\u30b0\u30e9\u30df\u30f3\u30b0\u3067\u306f\u3042\u307e\u308a\u4f7f\u3046\u3082\u306e\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u304c\u3001\u9069\u5207\u306b\u4f7f\u3046\u3053\u3068\u3067\u3088\u308a\u7c21\u6f54\u306a\u51e6\u7406\u3092\u66f8\u304f\u3053\u3068\u304c\u3067\u304d\u308b\u306e\u3067\u3001\u4f7f\u3048\u308b\u6a5f\u4f1a\u304c\u3042\u308b\u304b\u3069\u3046\u304b\u610f\u8b58\u3057\u3066\u307f\u308b\u3068\u3088\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n
\nhttps:\/\/github.com\/bbj14\/go-tag-example\/tree\/v1<\/a><\/p>\n\u80cc\u666f<\/h2>\n
type Person struct {\n Name string\n Age int64\n Weight float64\n BirthDate time.Time\n}<\/code><\/pre>\n
\nexcelize\u3067\u306fGetRows\u3068\u3044\u3046\u30e1\u30bd\u30c3\u30c9\u304c\u7528\u610f\u3055\u308c\u3066\u304a\u308a\u3001\u3053\u308c\u3092\u4f7f\u3046\u3053\u3068\u306b\u3088\u3063\u3066\u30b7\u30fc\u30c8\u306e\u30bb\u30eb\u5168\u4f53\u3092[][]string\u306b\u5909\u63db\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n f, err := excelize.OpenFile(".\/sample.xlsx")\n rows, err := f.GetRows("Sheet1")<\/code><\/pre>\n
\/\/ \u884c\u304b\u3089\u7279\u5b9a\u306e\u5217\u306e\u5024\u3092\u53d6\u5f97\nfunc getCol(row []string, col string) (string, error) {\n c, err := excelize.ColumnNameToNumber(col)\n if err != nil {\n return "", err\n }\n \/\/ \u884c\u306e\u6700\u5f8c\u306e\u7a7a\u6b04\u306fGetRows\u3067\u8aad\u307f\u8fbc\u307e\u308c\u306a\u3044\u305f\u3081\u3001\u305d\u3053\u3092\u53c2\u7167\u3057\u3066\u3044\u308b\u5834\u5408\u306f\u7a7a\u6587\u5b57\u3092\u8fd4\u3059\n if len(row) < c {\n return "", nil\n }\n return row[c-1], nil\n}<\/code><\/pre>\n
for _, row := range rows[1:] {\n name, err := getCol(row, "A")\n age, err := getCol(row, "B")\n weight, err := getCol(row, "C")\n birthdate, err := getCol(row, "D")\n\n p := Person{}\n\n p.Name = name\n p.Age, err = strconv.ParseInt(age, 10, 64)\n p.Weight, err = strconv.ParseFloat(weight, 64)\n p.Birthdate, err = dateparse.ParseAny(birthdate)\n\n \/\/ (p\u3092\u4f7f\u3063\u305f\u51e6\u7406\u3092\u66f8\u304f)\n }\n<\/code><\/pre>\n
\n
\u69cb\u9020\u4f53\u306e\u30bf\u30b0\u306b\u3064\u3044\u3066<\/h2>\n
\n\u3088\u304f\u76ee\u306b\u3059\u308b\u30bf\u30b0\u3068\u3057\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306ajson\u30bf\u30b0\u304c\u3042\u308b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\ntype Person struct {\n Name string `json:"name"`\n Age int64 `json:"age"`\n Weight float64 `json:"weight"`\n Birthdate time.Time `json:"birth_date"`\n}<\/code><\/pre>\n
type Person struct {\n Name string `column:"A"`\n Age int64 `column:"B"`\n Weight float64 `column:"C"`\n BirthDate time.Time `column:"D"`\n}<\/code><\/pre>\n
\u5b9f\u88c5<\/h2>\n
\n\u5404\u884c\u306e\u5024\u304c\u5165\u3063\u3066\u3044\u308b[]string\u3092\u69cb\u9020\u4f53\u306e\u5404\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5272\u308a\u5f53\u3066\u308b\u95a2\u6570\u306f\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n\/\/ \u69cb\u9020\u4f53\u306e\u30bf\u30b0\u3092\u5143\u306brow\u306e\u5024\u3092\u5272\u308a\u5f53\u3066\u308b\nfunc unmarshalRows(row []string, v any) error {\n var f func(reflect.Value) error\n f = func(v reflect.Value) error {\n for i := 0; i < v.NumField(); i++ {\n structField := v.Type().Field(i)\n field := v.Field(i)\n\n \/\/ \u69cb\u9020\u4f53\u304c\u57cb\u3081\u8fbc\u307e\u308c\u3066\u3044\u305f\u5834\u5408\u3001\u518d\u5e30\u7684\u306b\u63a2\u7d22\n if structField.Anonymous {\n f(field)\n continue\n }\n\n \/\/ column\u30bf\u30b0\u306e\u5024\u3092\u53d6\u5f97\n col := structField.Tag.Get("column")\n if col == "" {\n continue\n }\n\n str, err := getCol(row, col)\n if err != nil {\n return err\n }\n\n var val any\n\n \/\/ string\u3092\u30d5\u30a3\u30fc\u30eb\u30c9\u306e\u578b\u306b\u5408\u3046\u3088\u3046\u306b\u5909\u63db\n switch field.Interface().(type) {\n case string:\n val = str\n case int64:\n val, err = strconv.ParseInt(str, 10, 64)\n case float64:\n val, err = strconv.ParseFloat(str, 64)\n case time.Time:\n val, err = dateparse.ParseAny(str)\n default:\n return fmt.Errorf("unexpected type: %s", field.Type())\n }\n if err != nil {\n log.Println(err)\n continue\n }\n\n \/\/ \u69cb\u9020\u4f53\u306e\u30d5\u30a3\u30fc\u30eb\u30c9\u306b\u5024\u3092\u30bb\u30c3\u30c8\n field.Set(reflect.ValueOf(val))\n }\n\n return nil\n }\n\n if err := f(reflect.ValueOf(v).Elem()); err != nil {\n return err\n }\n\n return nil\n}<\/code><\/pre>\n
\n
\nhttps:\/\/cs.opensource.google\/go\/go\/+\/refs\/tags\/go1.18.4:src\/encoding\/json\/decode.go;l=96<\/a><\/p>\ntype Teacher struct {\n Person\n\n Class int64 `column:"E"`\n Subject string `column:"F"`\n}<\/code><\/pre>\n
func main() {\n f, err := excelize.OpenFile(".\/sample.xlsx")\n if err != nil {\n log.Fatal(err)\n }\n\n rows, err := f.GetRows("Sheet1")\n if err != nil {\n log.Fatal(err)\n }\n\n \/\/ 2\u884c\u76ee\u4ee5\u964d\u306b\u5bfe\u3057\u3066\n for _, row := range rows[1:] {\n p := Person{}\n if err := unmarshalRows(row, &p); err != nil {\n log.Fatal(err)\n }\n\n fmt.Printf("%+v\\n", p)\n }\n}<\/code><\/pre>\n
$ go run main.go\n{Name:\u4f50\u85e4 Age:10 Weight:42.7 Birthdate:2011-06-05 00:00:00 +0000 UTC}\n{Name:\u9234\u6728 Age:20 Weight:57.2 Birthdate:2001-09-21 00:00:00 +0000 UTC}\n{Name:\u9ad8\u6a4b Age:30 Weight:68.3 Birthdate:1991-07-19 00:00:00 +0000 UTC}\n{Name:\u7530\u4e2d Age:40 Weight:58.9 Birthdate:1981-03-06 00:00:00 +0000 UTC}\n{Name:\u4f0a\u85e4 Age:50 Weight:72.5 Birthdate:1971-11-08 00:00:00 +0000 UTC}<\/code><\/pre>\n
\u307e\u3068\u3081<\/h2>\n
\u53c2\u8003\u306b\u3057\u305f\u60c5\u5831<\/h2>\n