注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

用Go语言计算一个人的年龄,生肖,星座

go 水墨上仙 3758次浏览 已收录 手机上查看

go计算一个人的年龄,生肖,星座,输入参数为用户的出生年月日(类型string ,格式"2006-05-04").

package main

import (
  "fmt"
  "time"
)

func GetTimeFromStrDate(date string) (year, month, day int) {
    const shortForm = "2006-01-02"
    d, err := time.Parse(shortForm, date)
    if err != nil {
        fmt.Println("出生日期解析错误!")
        return 0,0,0
    }
    year = d.Year()
    month = int(d.Month())
    day = d.Day()
    return
}

func GetZodiac(year int) (zodiac string) {
    if year <= 0 {
        zodiac = "-1"
    }
    start := 1901
    x := (start - year) % 12
    if x == 1 || x == -11 {
        zodiac = "鼠"
    }
    if x == 0 {
        zodiac = "牛"
    }
    if x == 11 || x == -1 {
        zodiac = "虎"
    }
    if x == 10 || x == -2 {
        zodiac = "兔"
    }
    if x == 9 || x == -3 {
        zodiac = "龙"
    }
    if x == 8 || x == -4 {
        zodiac = "蛇"
    }
    if x == 7 || x == -5 {
        zodiac = "马"
    }
    if x == 6 || x == -6 {
        zodiac = "羊"
    }
    if x == 5 || x == -7 {
        zodiac = "猴"
    }
    if x == 4 || x == -8 {
        zodiac = "鸡"
    }
    if x == 3 || x == -9 {
        zodiac = "狗"
    }
    if x == 2 || x == -10 {
        zodiac = "猪"
    }
    return
}

func GetAge(year int) (age int) {
    if year <= 0 {
        age = -1
    }
    nowyear := time.Now().Year()
    age = nowyear - year
    return
}

func GetConstellation(month, day int) (star string) {
    fmt.Println("hello")
    if month <= 0 || month >= 13 {
        star = "-1"
    }
    if day <= 0 || day >= 32 {
        star = "-1"
    }
    if (month == 1 && day >= 20) || (month == 2 && day <= 18) { star = "水瓶座" } if (month == 2 && day >= 19) || (month == 3 && day <= 20) { star = "双鱼座" } if (month == 3 && day >= 21) || (month == 4 && day <= 19) { star = "白羊座" } if (month == 4 && day >= 20) || (month == 5 && day <= 20) { star = "金牛座" } if (month == 5 && day >= 21) || (month == 6 && day <= 21) { star = "双子座" } if (month == 6 && day >= 22) || (month == 7 && day <= 22) { star = "巨蟹座" } if (month == 7 && day >= 23) || (month == 8 && day <= 22) { star = "狮子座" } if (month == 8 && day >= 23) || (month == 9 && day <= 22) { star = "处女座" } if (month == 9 && day >= 23) || (month == 10 && day <= 22) { star = "天秤座" } if (month == 10 && day >= 23) || (month == 11 && day <= 21) { star = "天蝎座" } if (month == 11 && day >= 22) || (month == 12 && day <= 21) { star = "射手座" } if (month == 12 && day >= 22) || (month == 1 && day <= 19) {
        star = "魔蝎座"
    }

    return star
}

func main() {
    y, m, d := GetTimeFromStrDate("1986-06-18")
    fmt.Println(GetAge(y))
    fmt.Println(GetConstellation(m, d))
    fmt.Println(GetZodiac(y))
}

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明用Go语言计算一个人的年龄,生肖,星座
喜欢 (0)
[开心洋葱]
分享 (0)
水墨上仙
关于作者:
水墨上仙
……