golang根據生日計算星座和屬相實例

根據生日計算星座

func GetZodiac(month, day int) string {
    month = month - 1
    var (
        DAY_ARR = [12]int{20, 19, 21, 20, 21, 22, 23, 23, 23, 24, 23, 22}
        ZODIACS = [13]string{"摩羯座", "水瓶座", "雙魚座", "白羊座", "金牛座", "雙子座", "巨蟹座", "獅子座", "處女座", "天秤座", "天蠍座", "射手座", "摩羯座"}
    )

    if day < DAY_ARR[month] {
        return ZODIACS[month]
    } else {
        return ZODIACS[month+1]
    }
}

根據生日計算屬相

func GetChineseZodiac(year int) string {
    var CHINESE_ZODIACS = [12]string{"鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊", "猴", "雞", "狗", "豬"}
    if year > 1900 {
        return CHINESE_ZODIACS[(year-1900)%len(CHINESE_ZODIACS)]
    } else {
        return ""
    }
}

源碼地址

golang 工具庫

https://github.com/xbmlz/gct

以上就是golang根據生日計算星座和屬相實例的詳細內容,更多關於golang計算星座屬相的資料請關註WalkonNet其它相關文章!

推薦閱讀: