GOLANG:复合文字使用无键字段

时间:2017-11-07 01:10:29

标签: go

我收到了以下代码:

package catalog
...
type Time time.Time
func (t Time) MarshalJSON() ([]byte, error) {
   got := time.Time(t)
   stamp := fmt.Sprintf("\"%s\"", got.In(time.UTC).Format("2006-01-02T15:04:05.000Z"))
   return []byte(stamp), nil
}

我想尝试使用它:

package main

func main() {
   ...
   t := *a.StartTime  <<== This returns a time.Time
   t2 := catalog.Time{t}
}

而且,我收到以下错误:

catalog.Time composite literal uses unkeyed fields
implicit assignment of unexported field 'wall' in catalog.Time literal
cannot use t (type time.Time) as type uint64 in field value
too few values in structure initializer
import (catalog ".../go-catalog-types.git")

我也试过:t2:= catalog.Time {Time:t}和其他几个变种。有什么建议吗?

由于

1 个答案:

答案 0 :(得分:4)

我认为你想做

t2 := catalog.Time(t)

您已将catalog.Time声明为underlying type time.Time的类型,因此要在catalog.Time(time.Time)之间进行转换,您需要执行type Time struct { time.Time }

目前你已经写好了,好像你有embedded type一样,只有你有了

def check_for_current_appt(appt_epoch, appt_duration):
    '''INPUT : appt_timestamp (int (epoch time)): start time for appointment
               appt_duration (int): duration of appointment in seconds
       OUTPUT : appt_underway (bool): True if appointment is currently underway'''

    now = time.time()
    appt_underway = 0 < (now - appt_epoch) < appt_duration
    return appt_underway

https://play.golang.org/p/zbwf6ZfvX3