温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Golang怎么自定义类型和方法集

发布时间:2023-02-24 16:56:52 来源:亿速云 阅读:97 作者:iii 栏目:开发技术

这篇文章主要介绍“Golang怎么自定义类型和方法集”,在日常操作中,相信很多人在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))
}

请问上述代码会输出什么:

1.编译错误

2.运行时panic

3.{}

4."2022-07-20T20:30:00.135693011+08:00"

很多人一定会选4吧,然而答案是3:

$ go run customize.go
 
{}

是不是很意外,MyTime就是time.Time,理论上应该也实现了json.Marshaler,为什么输出的是空的呢?

实际上这是最近某个群友遇到的问题,乍一看像是golang的bug,但其实还是没掌握语言的基本规则。

在深入下去之前,我们先问自己两个问题:

  • MyTime 真的是 Time 类型吗?

  • MyTime 真的实现了 json.Marshaler 吗?

对于问题1,只需要引用spec里的说明即可:

A named type is always different from any other type.

https://go.dev/ref/spec#Type_identity

意思是说,只要是type定义出来的类型,都是不同的(type alias除外),即使他们的underlying type是一样的,也是两个不同的类型。

那么问题1的答案就知道了,显然MyTime不是time.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)

现在问题2也有答案了:MyTime没有实现json.Marshaler。

那么对于一个没有实现json.Marshaler的类型,json是怎么序列化的呢?这里就不卖关子了,文档里有写,对于没实现Marshaler的类型,默认的流程使用反射获取所有非export的字段,然后依次序列化,我们再看看time的结构:

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
}

里面都是非公开字段,所以直接序列化后整个结果就是{}。当然,Time类型自己重新实现了json.Marshaler,所以可以正常序列化成我们期望的值。

而我们的MyTime没有实现整个接口,所以走了默认的序列化流程。

所以我们可以得出一个重要的结论:从某个类型A派生出的类型B,B并不能获得A的方法集中的任何一个。

想要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拾遗:嵌入类型

如果我实在需要派生出一种新的类型呢,通常在我们写一个通用模块的时候需要隐藏实现的细节,所以想要对原始类型进行一定的包装,这时该怎么办呢?

实际上我们可以让MyTime重新实现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不冲突了。

不管哪一种方案,都可以解决问题,根据自己的实际需求做选择即可。

到此,关于“Golang怎么自定义类型和方法集”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI