英文:
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
循环中以人类可读的格式打印账户记录。我可以获取结构体的内容,但是我不太确定如何使用结果中的wall
、ext
和loc
成员。
例如,将fmt.Printf("%#v", e.CreatedAt)
插入到for
循环中,会得到一个时间的表示形式,类似于:
coinbase.Time{wall:0x3015a123, ext:63612345678, loc:(*time.Location)(nil)} {806986000 63638738354 <nil>}
我还可以看到wall
的类型是uint64
,ext
的类型是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 "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
得分: 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("2006-01-02 15:04:05.999999+00")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论