如何在 go 中声明自定义类型(如 time.Date)的变量?

Til*_*lus 3 variables object go declare

我正在尝试创建这个代码块:

var nextWorkday time.Date
// var nextWorkday *time.Date // neither works
yyyy, mm, dd := now.Date()
goalTime, _ := time.ParseDuration(fmt.Sprintf("%fh", *goal))
goalSeconds := int(goalTime.Seconds())
if date.Weekday() != time.Friday { // wait till the next workday (7am + difference)
    nextWorkday = time.Date(yyyy, mm, dd+1, 7, 0, 0+goalSeconds, 0, now.Location())
} else {
    nextWorkday = time.Date(yyyy, mm, dd+3, 7, 0, 0+goalSeconds, 0, now.Location())
}
time.Sleep(nextWorkday)
Run Code Online (Sandbox Code Playgroud)

重要的断点已经是第一行。我不知道如何声明自定义类型的新变量。现在我收到错误: time.Date is not a type

我究竟做错了什么?任何帮助表示赞赏!

icz*_*cza 8

time.Date标准封装中没有类型time。然而,有一种time.Time类型表示一个时刻,“包括”日期。

time.Date()time.Time是一个根据提供的日期和时间字段构造值的函数。