1.定义结构
- // 结构定义
- type VideoFrame struct {
- id int
- head []byte
- len int64
- data []byte
- }
2.实现结构方法
- // 生成结构字段的get与set方法
- // ================================
- func (v *VideoFrame) Id() int {
- return v.id
- }
-
- func (v *VideoFrame) SetId(id int) {
- v.id = id
- }
-
- func (v *VideoFrame) Head() []byte {
- return v.head
- }
-
- func (v *VideoFrame) SetHead(head []byte) {
- v.head = head
- }
-
- func (v *VideoFrame) Len() int64 {
- return v.len
- }
-
- func (v *VideoFrame) SetLen(len int64) {
- v.len = len
- }
-
- func (v *VideoFrame) Data() []byte {
- return v.data
- }
-
- func (v *VideoFrame) SetData(data []byte) {
- v.data = data
- }
-
- //================================
3.定义接口
- // 接口定义
- type myInterface interface {
- getVideoFrame() *VideoFrame
- FrameCount() int
- }
4.结构实现接口方法
- //实现接口方法
-
- func (v VideoFrame) getVideoFrame() *VideoFrame {
- mybyte := make([]byte, 32)
- copy(mybyte, "hello")
- vf := VideoFrame{
- id: 1111,
- head: mybyte,
- len: 128,
- data: []byte{01, 23, 45, 67, 89},
- }
- return &vf
- }
-
- func (v VideoFrame) FrameCount() int {
- return 999
- }
5.使用接口与结构方法
- //使用接口方法
- vf := VideoFrame{}
- fmt.Println("===> FrameCount:", vf.FrameCount())
- fmt.Println("===>VideoFrame:", vf.getVideoFrame())
- //使用结构方法
- fmt.Println("===>VideoFrame->ID:", vf.Id())
- vf.SetId(8888)
- fmt.Println("===>VideoFrame-ID:", vf.Id())