[TOC]编程
201808bash
Golang里面没有像C++同样有继承相关的概念,可是咱们却能够实现继承相关的用法,这就要用到struct、interface这两个结构。微信
Golang里面有组合的概念,也就是一个struct 里面能够包含一个或者多个struct,struct能够近似理解为面向对象编程中的class,可是不能等同,有不少区别。若是一个struct实现了某个接口的全部方法,那么只要是包含这个struct的全部其余struct也都是实现了这个接口的全部方法session
要想实现class类的用法,那么就要用到struct结构,经过给定struct定义某个成员变量或成员方法就能够实现类的方法函数
经过type struct 定义一个struct【类】ui
type rsaSecurity struct {
}
复制代码
再定义一个这个类的变量,也就是对象加密
var RsaSecuritySrv rsaSecurity
复制代码
相似于构造函数的定义,也能够经过new一个对象来使用,二选一。spa
实现一个这个struct类的方法,须要注意要显示的声明所属对象,即(rs *rsaSecurity)
指针
// 加密
func (rs *rsaSecurity) RsaEncrypt(origData []byte) ([]byte, error) {
//解密pem格式的公钥
block, _ := pem.Decode(publicKey)
if block == nil {
return nil, errors.New("public key error")
}
// 解析公钥
pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
if err != nil {
return nil, err
}
// 类型断言
pub := pubInterface.(*rsa.PublicKey)
//加密
return rsa.EncryptPKCS1v15(rand.Reader, pub, origData)
}
复制代码
注意这里是否使用指针,在因而否可以是否须要完全修改为员变量的值。code
在任何须要调用这个成员方法的时候,经过对象来调用
func main() {
data, _ := RsaSecuritySrv.RsaEncrypt([]byte(encrypt))
}
复制代码
直接上代码以下,很简单,主要就是一个struct里面包含一个匿名的struct,也就是经过匿名组合来实现
package main
import (
"fmt"
)
// 【基类】
//定义一个最基础的struct类MsgModel,里面包含一个成员变量msgId
type MsgModel struct {
msgId int
msgType int
}
// MsgModel的一个成员方法,用来设置msgId
func (msg *MsgModel) SetId(msgId int) {
msg.msgId = msgId
}
func (msg *MsgModel) SetType(msgType int) {
msg.msgType = msgType
}
//【子类】
// 再定义一个struct为GroupMsgModel,包含了MsgModel,即组合,可是并无给定MsgModel任何名字,所以是匿名组合
type GroupMsgModel struct {
MsgModel
// 若是子类也包含一个基类的同样的成员变量,那么经过子类设置和获取获得的变量都是基类的
msgId int
}
func (group *GroupMsgModel) GetId() int {
return group.msgId
}
/*
func (group *GroupMsgModel) SetId(msgId int) {
group.msgId = msgId
}
*/
func main() {
group := &GroupMsgModel{}
group.SetId(123)
group.SetType(1)
fmt.Println("group.msgId =", group.msgId, "\tgroup.MsgModel.msgId =", group.MsgModel.msgId)
fmt.Println("group.msgType =", group.msgType, "\tgroup.MsgModel.msgType =", group.MsgModel.msgType)
}
复制代码
Golang能够经过匿名组合来实现继承。
Golang能够interface + struct来实现虚基类的用法,必需要实现interface中定义的方法。
1,定义一个interface接口MsgModel,包含了一些方法。
type MsgModel interface {
Persist(context context.Context, msg interface{}) bool
PersistOnSensitive(context context.Context, session_type, level, SensitiveStatus int32, msg interface{}) bool
}
复制代码
3,定义一个类型msgModelImpl,用来实现接口类型
定义一个struct用来实现接口类型
type msgModelImpl struct{}
定义一个变量MsgModelImpl等于msgModelImpl,至关于能够经过MsgModelImpl来调用msgModelImpl的成员
var MsgModelImpl = msgModelImpl{}
实现接口的两个方法
func (m msgModelImpl) Persist(context context.Context, msgIface interface{}) bool {
// 具体实现省略
}
func (m msgModelImpl) UpdateDbContent(context context.Context, msgIface interface{}) bool {
// 具体实现省略
}
复制代码
4, 定义一个struct类型的msgService,包含上述接口类型MsgModel,至关于组合了。这样的话,这个类型就须要要实现接口方法。
type msgService struct {
msgModel MsgModel
}
复制代码
5, 再定义一个变量MsgService,首字母大写,而且赋值为msgService对象,同时给成员msgModel赋值为上述已经实现了接口的struct对象MsgModelImpl。
将上述已经实现接口类型的类型(MsgModelImpl) 赋值给此变量(此变量而且要是包含了接口类型的类型), 而后这个变量就能够供外部调用
var MsgService = msgService{
msgModel: MsgModelImpl,
}
复制代码
6, 经过MsgService调用接口方法
7, 小结:
【"欢迎关注个人微信公众号:Linux 服务端系统研发,后面会大力经过微信公众号发送优质文章"】