douhui8454 2012-01-09 13:06
浏览 214
已采纳

将C转换(翻译)成Go的工具? [关闭]

What tool to use to convert C source code into Go source code?

For example, if the C code contains:

struct Node {
    struct Node *left, *right;
    void        *data;
};

char charAt(char *s, int i) {
    return s[i];
}

the corresponding Go code generated by the tool should be:

type Node struct {
    left, right *Node
    data        interface{}
}

func charAt(s string, i int) byte {
    return s[i]
}

The tool does not need to be perfect. It is OK if some parts of the generated Go code need to be corrected by hand.

  • 写回答

6条回答 默认 最新

  • duanmei1536 2012-01-09 13:27
    关注

    I guess no such (C to Go source code conversion) tool exist today. You might consider to make your own converter. The question becomes: is it worth it, and how to do that?

    It probably might not be worth the effort, because Go and C could be somehow interoperable. For example, if you use the GCC 4.6 (or to be released 4.7, i.e. the latest snapshot) your probably can link C & Go code together, with some care.

    Of course, as usual, the evil is in the details.

    If you want a converter, do you want the obtained Go code to be readable and editable (then the task is more difficult, since you want to keep the structure of the code, and you also want to keep the comments)? In that case, you probably need your own C parser (and it is a difficult task).

    If you don't care about readability of the generated Go code, you could for example extend an existing compiler to do the work. For example, GCC is extensible thru plugins or thru MELT extensions, and you could customize GCC (with MELT, or your own C plugin for GCC) to transform Gimple representation (the main internal representation for instructions inside GCC) to unreadable Go code. This is somehow simpler (but still require more than a week of work).

    Of course, Go interfaces, channels and even memory management (garbage collected memory) has no standard C counterpart.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号