duanmin0941 2017-09-12 16:26
浏览 123
已采纳

Golang中非原子类型的类型包装

I'm new to golang and am trying to understand a code example of type wrapping for the "non-atomic" type time.Time.

The type extension in question is from the Go client for GDAX on github, go-coinbase-exchange project.

The expected behavior would be for Time variables from the project (coinbase.Time), which are of type Time time.Time (as defined in the project's time.go file) to behave something like the following example for extending the "atomic" type int (from blog.riff.org in that they might follow a kind of "inheritance" from the base type for functions like Time.format (from golang's standard implementation of time:

package main
import "fmt"
type Int int

func (i Int) Add(j Int) Int {
  return i + j
}

func main() {
  i := Int(5)
  j := Int(6)
  fmt.Println(i.Add(j))
  fmt.Println(i.Add(j) + 12)
}

But if I modify the code example from the project's List Account Ledger example found in Readme.md to include a print function which might otherwise give me a human-readable view of the CreatedAt struct variables (as follows), I get a compiler error saying that "type coinbase.Time has no field or method Format":

for _, e := range ledger {
    print("Entry Creation: ")
    fmt.Printf(e.CreatedAt.Format("2006-01-02 15:04:05.999999+00"))
}

The expected behavior inside the for loop would be for it to print the ledger entries in a human-readable format. I can get the contents of the structs, but I'm not really sure how to then use the resulting wall, ext and loc members.

For example, inserting fmt.Printf("%#v", e.CreatedAt) into the for loop gives me a representation of the time that looks something like this:

coinbase.Time{wall:0x3015a123, ext:63612345678, loc:(*time.Location)(nil)} {806986000 63638738354 <nil>}

I can also see that wall is of type uint64, that ext is of type int64 and that loc is just GMT/UTC=0 by formatting the variable as a string because fmt.Printf("%s", e.CreatedAt) gives me output which is similar to the following:

{%!s(uint64=712345678) %!s(int64=63612345678) %!s(*time.Location=<nil>)}

It seems like I'm missing something. I've requested further information through issues tab on github, but maybe this is a nube question. So I'm not sure how quick the response time would be, and I'm interested in the more general case for extending non-atomic types in go.

  • 写回答

1条回答 默认 最新

  • dtvgo28624 2017-09-12 16:38
    关注

    Named types do not inherit any methods from the underlying type (indeed there is no inheritance at all in Go); you must cast to the underlying type to call any methods from that type:

    (time.Time(e.CreatedAt)).Format("2006-01-02 15:04:05.999999+00")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来