dongxun4110 2017-11-07 01:10 采纳率: 0%
浏览 35

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

I've been given the following code:

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
}

and I'm trying to use it like:

package main

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

And, I get the following error:

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")

I've also tried: t2 := catalog.Time{Time: t} and several other variations. Any suggestions?

Thanks

  • 写回答

1条回答 默认 最新

  • dongzheng7165 2017-11-07 01:13
    关注

    I think you are wanting to do

    t2 := catalog.Time(t)
    

    You have declared catalog.Time as a type with an underlying type of time.Time, so to convert between them you need to do catalog.Time(time.Time).

    Currently you have written it as if you had an embedded type, which would only work if you had

    type Time struct {
        time.Time
    }
    

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

    评论

报告相同问题?

悬赏问题

  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表