我写了一个 go interface 相关的代码转换为 C 代码的样例。也许有助于大家理解 go 的 interface。不过请注意一点,这里没有完整解析 go 语言 interface 的所有细节。

 
Go 代码:
 
package main

import "fmt"

// -------------------------------------------------------------

type IReadWriter interface {
    Read(buf *byte, cb int) int
    Write(buf *byte, cb int) int
}

// -------------------------------------------------------------

type A struct {
    a int
}

func NewA(params int) *A {
    fmt.Println("NewA:", params);
    return &A{params}
}

func (this *A) Read(buf *byte, cb int) int {
    fmt.Println("A_Read:", this.a)
    return cb
}

func (this *A) Write(buf *byte, cb int) int {
    fmt.Println("A_Write:", this.a)
    return cb
}

// -------------------------------------------------------------

type B struct {
    A
}

func NewB(params int) *B {
    fmt.Println("NewB:", params);
    return &B{A{params}}
}

func (this *B) Write(buf *byte, cb int) int {
    fmt.Println("B_Write:", this.a)
    return cb
}

func (this *B) Foo() {
    fmt.Println("B_Foo:", this.a)
}

// -------------------------------------------------------------

func main() {
    var p IReadWriter = NewB(8)
    p.Read(nil, 10)
    p.Write(nil, 10)
}

// -------------------------------------------------------------

 
 
对应的 C 代码:
 
 
#include <stdio.h>
#include <stdlib.h>

// -------------------------------------------------------------

typedef struct _TypeInfo {
    // 用于运行时取得类型信息, 比如反射机制
} TypeInfo;

typedef struct _InterfaceInfo {
    // 用于运行时取得interface信息
} InterfaceInfo;

// -------------------------------------------------------------

typedef struct _IReadWriterTbl {
    InterfaceInfo* inter;
    TypeInfo* type;
    int (*Read)(void* this, char* buf, int cb);
    int (*Write)(void* this, char* buf, int cb);
} IReadWriterTbl;

typedef struct _IReadWriter {
    IReadWriterTbl* tab;
    void* data;
} IReadWriter;

InterfaceInfo g_InterfaceInfo_IReadWriter = {
    // ...    
};

// -------------------------------------------------------------

typedef struct _A {
    int a;
} A;

int A_Read(A* this, char* buf, int cb) {
    printf("A_Read: %d\n", this->a);
    return cb;
}

int A_Write(A* this, char* buf, int cb) {
    printf("A_Write: %d\n", this->a);
    return cb;
}

TypeInfo g_TypeInfo_A = {
    // ...    
};

A* NewA(int params) {
    printf("NewA: %d\n", params);
    A* this = (A*)malloc(sizeof(A));
    this->a = params;
    return this;
}

// -------------------------------------------------------------

typedef struct _B {
    A base;
} B;

int B_Write(B* this, char* buf, int cb) {
    printf("B_Write: %d\n", this->base.a);
    return cb;
}

void B_Foo(B* this) {
    printf("B_Foo: %d\n", this->base.a);
}

TypeInfo g_TypeInfo_B = {
    // ...    
};

B* NewB(int params) {
    printf("NewB: %d\n", params);
    B* this = (B*)malloc(sizeof(B));
    this->base.a = params;
    return this;
}

// -------------------------------------------------------------

IReadWriterTbl g_Itbl_IReadWriter_B = {
    &g_InterfaceInfo_IReadWriter,
    &g_TypeInfo_B,
    (int (*)(void* this, char* buf, int cb))A_Read,
    (int (*)(void* this, char* buf, int cb))B_Write
};

int main() {

    B* unnamed = NewB(8);
    IReadWriter p = {
        &g_Itbl_IReadWriter_B,
        unnamed
    };

    p.tab->Read(p.data, NULL, 10);
    p.tab->Write(p.data, NULL, 10);

    return 0;
}

// -------------------------------------------------------------

http://blog.csdn.net/xushiweizh/article/details/7034346

浅解 go 语言的 interface(许的博客)的更多相关文章

  1. iOS中 视频直播功能-流媒体的使用(详解)韩俊强的CSDN博客

    上一篇博客:(流媒体实现视频播放和下载功能):http://blog.csdn.net/qq_31810357/article/details/50574914 最近视频直播功能比较火,处于需求,研究 ...

  2. 2018上C语言程序设计(高级)博客作业样例

    要求一(20分) 完成PTA中题目集名为<usth-C语言高级-第1次作业>中的所有题目. 要求二 PTA作业的总结(20分+30分) 将PTA第1次作业作业中以下2道题的解题思路按照规定 ...

  3. WebConfig配置文件详解(转载自逆心的博客)

    <?xml version="1.0"?> <!--注意: 除了手动编辑此文件以外,您还可以使用 Web 管理工具来配置应用程序的设置.可以使用 Visual S ...

  4. iOS中 支付宝钱包详解/第三方支付 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博! iOS开发者交流QQ群: 446310206 一.在app中成功完成支付宝支付的过程 1.申请支付宝钱包.参考网址 ...

  5. 【转】log4j.properties 详解与配置步骤 - edward0830ly的专栏 - 博客频道 - CSDN.NET

    一.log4j.properties 的使用详解 1.输出级别的种类 ERROR.WARN.INFO.DEBUGERROR 为严重错误 主要是程序的错误WARN 为一般警告,比如session丢失IN ...

  6. javascript工具--控制台详解(转自 阮一峰博客)

    大神这篇博客是写在2011年,主要介绍 “Firefox” 浏览器插件 “Firebug” 的操作,如今主流浏览器对控制台都已经提供了很好的支持.我自己用的最多是谷歌的 “chrome” 浏览器,下面 ...

  7. 微博爬虫“免登录”技巧详解及 Java 实现(业余草的博客)

    一.微博一定要登录才能抓取? 目前,对于微博的爬虫,大部分是基于模拟微博账号登录的方式实现的,这种方式如果真的运营起来,实际上是一件非常头疼痛苦的事,你可能每天都过得提心吊胆,生怕新浪爸爸把你的那些账 ...

  8. C语言I博客作业02

    这个作业属于那个课程  C语言程序设计I 这个作业要求在哪 https://edu.cnblogs.com/campus/zswxy/CST2019-3/homework/8656 我在这个课程的目标 ...

  9. c语言1博客作业02

    c语言1博客作业02 这个作业属于哪个课程 C语言程序设计 这个作业的要求在哪 [作业要求](https://edu.cnblogs.com/campus/zswxy/SE2019-2/homewor ...

随机推荐

  1. python ASCII编码集

  2. python元组和range

    1.元组 1)元组介绍 元组: 俗称不可变的列表.⼜被成为只读列表, 元组也是python的基本数据类型之⼀, ⽤⼩括号括起来, ⾥⾯可以放任何数据类型的数据, 查询可以. 循环也可以. 切片也可以. ...

  3. windows 和 linux 安装 tensorflow

    安装 跟往常一样,我们用 Conda 来安装 TensorFlow.你也许已经有了一个 TensorFlow 环境,但要确保你安装了所有必要的包. OS X 或 Linux 运行下列命令来配置开发环境 ...

  4. python2和python3一些不同

    文件写法: #python2fp=file(filepath,'wb')#python3# fp = open(filepath, 'wb') 关于乱码问题: #python2 #coding=utf ...

  5. mysql 字段名和关键字冲突

    用"(`)"将有冲突的字段框起来,,键盘上1边上那个键. 例: SELECT * FROM yun_roleright WHERE right LIKE '%{13}%'; 上面s ...

  6. Project Euler Problem 18-Maximum path sum I & 67-Maximum path sum II

    基础的动态规划...数塔取数问题. 状态转移方程: dp[i][j] = num[i][j] + max(dp[i+1][j],dp[i+1][j+1]);

  7. 2013-2-1 pdf中无法用金山词霸取词问题

    打开pdf的编辑——〉首选项——〉一般——〉选项——〉开始——〉只有经过认证的插件,把‘checkbox’里的勾去掉,重启. ★在acrobat reader启动画面里如果没有加载xdict32(工具 ...

  8. oracle用WHERE替代ORDER BY

    ORDER BY 子句只在两种严格的条件下使用索引. ORDER BY中所有的列必须包含在相同的索引中并保持在索引中的排列顺序. ORDER BY中所有的列必须定义为非空. WHERE子句使用的索引和 ...

  9. Android Animation动画详解(一): 补间动画

    前言 你有没有被一些APP中惊艳的动画效果震撼过,有没有去思考,甚至研究过这些动画是如何实现的呢? 啥?你没有思考,更没有研究过? 好吧,那跟着我一起来学习下如何去实现APP中那些让我们惊羡的动画特效 ...

  10. C#的选择语句练习(二)

    1.输入a,b,c三个数,计算一元二次方程ax²+bx+c的根:若a=0,则不是一元二次方程:△=b²-4ac,根的计算公式为-b±√b²-4ac/2a:若△=b²-4ac>0,则方程有两个不一 ...