Golang学习摘录(三)
发布时间:2020-12-16 18:33:53 所属栏目:大数据 来源:网络整理
导读:golang学习笔记(三) 方法(Method sets) The method set of any other type T consists of all methods declared with receiver type T. The method set of the corresponding pointer type *T is the set of all methods declared with receiver *T or T
golang学习笔记(三)
2.继承 golang继承很隐晦,类型的聚合从而达到方法的继承。比如Mutex类型有两个方法,Lock和Unlock. type Mutex struct { //省略其字段 } func (m *Mutex) Lock() { //省略其实现 } func (m *Mutex)Unlock() { //省略其实现 } 现在我们通过以下两种方式创建新的数据类型 type NewMutex Mutex type PrintableMutex struct {Mutex} NewMutex是Mutex别名,不同的类型,但是NewMutex不包含任何Mutex的方法,也就是NewMutex的方法是空的。但是PrintableMutex从Mutex继承了以上2个方法,并且绑定到PrintableMutex匿名字段Mutex上。 对于接口类型,别名即可继承。 type Block interface { BlockSize() int Encrypt(src,dst []byte) Decrypt(src,dst []byte) } type MyBlock Block //MyBlock继承Block的3个方法 3.goroutine并行执行 并发,并行二者区别,可以自行百度。goroutine是并发执行的,但是默认情况下,不是并行运行,也就是说任意时刻,只有一个goroutine运行。可以设置runtime.GOMAXPROCS来设置几个goroutine并行执行,一般设置为CPU个数,每个goroutine分到一个CPU。后续版本golang改进调度器后,能实现自动CPU绑定,并行执行了,就会取消此设置项。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |