Type wrapping of non-atomic types in golang

huangapple go评论103阅读模式
英文:

Type wrapping of non-atomic types in golang

问题

我是你的中文翻译助手,以下是你提供的内容的翻译:

我对golang还不熟悉,正在尝试理解一个关于"time.Time"类型封装的代码示例。

这个问题中涉及到的类型扩展来自于GitHub上的Go客户端GDAX,即go-coinbase-exchange项目

期望的行为是,项目中的Time变量(coinbase.Time)应该表现得像以下示例中扩展了"atomic"类型int的行为一样(来自blog.riff.org),它们可能会遵循一种从基本类型继承的方式,比如Time.format函数(来自golang标准时间实现):

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

但是,如果我修改项目中的"List Account Ledger"示例(在Readme.md中找到),添加一个打印函数,该函数本来可以给我提供CreatedAt结构体变量的可读视图(如下所示),我会得到一个编译器错误,提示"type coinbase.Time没有Format字段或方法":

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

期望的行为是,在for循环中以人类可读的格式打印账户记录。我可以获取结构体的内容,但是我不太确定如何使用结果中的wallextloc成员。

例如,将fmt.Printf("%#v", e.CreatedAt)插入到for循环中,会得到一个时间的表示形式,类似于:

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

我还可以看到wall的类型是uint64ext的类型是int64,而loc只是GMT/UTC=0,通过将变量格式化为字符串,因为fmt.Printf("%s", e.CreatedAt)给出的输出类似于以下内容:

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

我觉得我可能漏掉了什么。我已经通过GitHub上的问题选项卡请求了进一步的信息,但也许这是一个初学者的问题。所以我不确定回复的速度会有多快,我对于在go中扩展非原子类型的更一般的情况很感兴趣。

英文:

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 &quot;fmt&quot;
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(&quot;Entry Creation: &quot;)
	fmt.Printf(e.CreatedAt.Format(&quot;2006-01-02 15:04:05.999999+00&quot;))
}

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(&quot;%#v&quot;, 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 &lt;nil&gt;}

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(&quot;%s&quot;, e.CreatedAt) gives me output which is similar to the following:

{%!s(uint64=712345678) %!s(int64=63612345678) %!s(*time.Location=&lt;nil&gt;)}

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

得分: 1

命名类型 不会从底层类型继承任何方法(实际上,在Go语言中根本没有继承);你必须将其转换为底层类型才能调用该类型的任何方法:

(time.Time(e.CreatedAt)).Format("2006-01-02 15:04:05.999999+00")
英文:

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(&quot;2006-01-02 15:04:05.999999+00&quot;)

huangapple
  • 本文由 发表于 2017年9月13日 00:26:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/46181419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定