Help us understand the problem. What is going on with this article?

Go言語 13日の金曜日をjson形式で出力する

More than 3 years have passed since last update.

Qiita - 13日の金曜日を返すプログラム

こちらの記事を見て、面白そうだったのと暇だったので作ってみた次第です。

エンジニアにとっては、13日の金曜日はJSONと戯れる日として認知されているようです。
13日の金曜日 json - Google 検索

jason.go
package main

import (
    "encoding/json"
    "errors"
    "fmt"
    "io/ioutil"
    "os"
)

type Jason struct {
    Year  int
    Month []int
}

func yobi(year int, month int) (int, error) {
    if year < 0 {
        return 0, errors.New("input error!!")
    }

    if month == 1 || month == 2 {
        year -= 1
        month += 12
    }
    return (year + year/4 - year/100 + year/400 + (13*month+8)/5 + 1) % 7, nil
}

func EncodingJSON(jason Jason) []byte {
    bdata, err := json.Marshal(jason)

    if err != nil {
        fmt.Println(err)
        return nil
    }
    return bdata
}

func main() {
    var (
        year       int
        pyobi      int
        jasonMonth []int
        err        error
    )
    fmt.Printf("Please, input year.->")
    fmt.Scan(&year)

    for month := 1; month <= 12; month++ {
        pyobi, err = yobi(year, month)

        if err != nil {
            fmt.Println(err)
            return
        }

        if pyobi == 0 {
            jasonMonth = append(jasonMonth, month)
        }
    }

    jason := Jason{year, jasonMonth}
    bdata := EncodingJSON(jason)
    os.Stdout.Write(bdata)

    content := []byte(bdata)
    ioutil.WriteFile("jason.json", content, os.ModePerm)
}

gistはこちら

Usage

13日の金曜日を調べたい西暦を入力して下さい。
例)2015年の場合

$ go run jason.go
Please, input year.->2015
{"Year":2015,"Month":[2,3,11]}

実行すると jason.json ファイルが出来上がります。

$ ls
jason.go jason.json
$ cat jason.json 
{"Year":2015,"Month":[2,3,11]}

一応、0以下の数値を与えるとエラーになります。
おわりです。

参考

KOBE GDG: Go言語でJSONを扱う, http://kobegdg.blogspot.jp/2013/03/gojson.html
Go言語でファイル操作: ファイルを作って書き込む - Qiita -, http://qiita.com/suin/items/7eb4fc405ac73846a9b1

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away