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

  • 本文关键字:字段 复合 文字 GOLANG 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 := 目录。时间{时间:t}和其他几种变体。 有什么建议吗?

谢谢

我想你想做

t2 := catalog.Time(t)

您已将catalog.Time声明为具有基础类型time.Time的类型,因此要在它们之间进行转换,您需要执行catalog.Time(time.Time)

目前,您已经编写了它,就好像您有一个嵌入类型一样,它只有在您有

type Time struct {
    time.Time
}

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

最新更新