golang拾遗主要是用来记录一些遗忘了的、平时从没注意过的golang相关知识。
很久没更新了,我们先以一个谜题开头练练手:
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "time" | |
| ) | |
| type MyTime time.Time | |
| func main() { | |
| myTime := MyTime(time.Now()) // 假设获得的时间是 2022年7月20日20:30:00,时区UTC+8 | |
| res, err := json.Marshal(myTime) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(string(res)) | |
| } |
请问上述代码会输出什么:
- 编译错误
- 运行时panic
- {}
- "2022-07-20T20:30:00.135693011+08:00"
很多人一定会选4吧,然而答案是3:
| $ go run customize.go | |
| {} |
MyTimetime.Timejson.Marshaler
实际上这是最近某个群友遇到的问题,乍一看像是golang的bug,但其实还是没掌握语言的基本规则。
在深入下去之前,我们先问自己两个问题:
json.Marshaler
对于问题1,只需要引用spec里的说明即可:
A named type is always different from any other type.
意思是说,只要是type定义出来的类型,都是不同的(type alias除外),即使他们的underlying type是一样的,也是两个不同的类型。
MyTimetime.Time
既然MyTime不是Time,那它是否能用Time类型的method呢?毕竟MyTime的基底类型是Time呀。我们写段代码验证下:
| package main | |
| import ( | |
| "fmt" | |
| "time" | |
| ) | |
| type MyTime time.Time | |
| func main() { | |
| myTime := MyTime(time.Now()) // 假设获得的时间是 2022年7月20日20:30:00,时区UTC+8 | |
| res, err := myTime.MarsharlJSON() | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(string(res)) | |
| } |
运行结果:
| # command-line-arguments | |
| ./checkoutit.go:12:24: myTime.MarsharlJSON undefined (type MyTime has no field or method MarsharlJSON) |
MyTimejson.Marshaler
json.MarshalerMarshaler
| type Time struct { | |
| // wall and ext encode the wall time seconds, wall time nanoseconds, | |
| // and optional monotonic clock reading in nanoseconds. | |
| // | |
| // From high to low bit position, wall encodes a 1-bit flag (hasMonotonic), | |
| // a 33-bit seconds field, and a 30-bit wall time nanoseconds field. | |
| // The nanoseconds field is in the range [0, 999999999]. | |
| // If the hasMonotonic bit is 0, then the 33-bit field must be zero | |
| // and the full signed 64-bit wall seconds since Jan 1 year 1 is stored in ext. | |
| // If the hasMonotonic bit is 1, then the 33-bit field holds a 33-bit | |
| // unsigned wall seconds since Jan 1 year 1885, and ext holds a | |
| // signed 64-bit monotonic clock reading, nanoseconds since process start. | |
| wall uint64 | |
| ext int64 | |
| // loc specifies the Location that should be used to | |
| // determine the minute, hour, month, day, and year | |
| // that correspond to this Time. | |
| // The nil location means UTC. | |
| // All UTC times are represented with loc==nil, never loc==&utcLoc. | |
| loc *Location | |
| } |
{}json.Marshaler
而我们的MyTime没有实现整个接口,所以走了默认的序列化流程。
所以我们可以得出一个重要的结论:从某个类型A派生出的类型B,B并不能获得A的方法集中的任何一个。
type B A
方法一是使用type alias:
| - type MyTime time.Time | |
| + type MyTime = time.Time | |
| func main() { | |
| - myTime := MyTime(time.Now()) // 假设获得的时间是 2022年7月20日20:30:00,时区UTC+8 | |
| + var myTime MyTime = time.Now() // 假设获得的时间是 2022年7月20日20:30:00,时区UTC+8 | |
| res, err := json.Marshal(myTime) | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(string(res)) | |
| } |
类型别名自如其名,就是创建了一个类型A的别名而没有定义任何新类型(注意那两行改动)。现在MyTime就是Time了,自然也可以直接利用Time的MarshalJSON。
方法二,使用内嵌类型:
| - type MyTime time.Time | |
| + type MyTime struct { | |
| + time.Time | |
| + } | |
| func main() { | |
| - myTime := MyTime(time.Now()) // 假设获得的时间是 2022年7月20日20:30:00,时区UTC+8 | |
| + myTime := MyTime{time.Now} | |
| res, err := myTime.MarsharlJSON() | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(string(res)) | |
| } |
通过将Time嵌入MyTime,MyTime也可以获得Time类型的方法集。更具体的可以看我之前写的另一篇文章:golang拾遗:嵌入类型
如果我实在需要派生出一种新的类型呢,通常在我们写一个通用模块的时候需要隐藏实现的细节,所以想要对原始类型进行一定的包装,这时该怎么办呢?
json.Marshaler
| type MyTime time.Time | |
| func (m MyTime) MarshalJSON() ([]byte, error) { | |
| // 我图方便就直接复用Time的了 | |
| return time.Time(m).MarshalJSON() | |
| } | |
| func main() { | |
| myTime := MyTime(time.Now()) // 假设获得的时间是 2022年7月20日20:30:00,时区UTC+8 | |
| res, err := myTime.MarsharlJSON() | |
| if err != nil { | |
| panic(err) | |
| } | |
| fmt.Println(string(res)) | |
| } |
这么做看上去违反了DRY原则,其实未必,这里只是示例写的烂而已,真实场景下往往对派生出来的自定义类型进行一些定制,因此序列化函数里会有额外的一些操作,这样就和DRY不冲突了。
不管哪一种方案,都可以解决问题,根据自己的实际需求做选择即可。
总结
- 直接定义在B上的那些方法
- 作为嵌入类型包含在B里的其他类型的方法
而A的方法是不存在在B中的。
type B struct {a, b int}
- 直接定义在B上的那些方法
还有最重要的,如果两个类型名字不同,即使它们的结构完全相同,也是两个不同的类型。
这些边边角角的知识很容易被遗忘,但还是有机会在工作中遇到的,记牢了可以省很多事。
转 https://www.cnblogs.com/apocelipes/p/16500493.html