duanjia6959 2014-12-16 03:00
浏览 178
已采纳

在Go中嵌入脚本语言

Is it possible to embed a language inside Go? With example on how the embedded language could access Go's variables. I need it to create plugins inside my application.

  • 写回答

4条回答 默认 最新

  • dtgvl48608 2014-12-16 03:08
    关注

    At the first, I'll explain cgo. Go provides API to export values into C language.

    http://golang.org/cmd/cgo/

    For example, you can export string as char* like below.

    package main
    
    /*
    #include <stdio.h>
    static void myputs(char* s) {
        puts(s);
    }
    */
    import "C"
    
    func main() {
        s := "hello world"
        C.myputs(C.CString(s))
    }
    

    So you need to write functions to access C library. But there are some packages to use script languages. See:

    https://github.com/mattn/go-mruby

    https://github.com/mattn/go-v8

    Or if you don't want to use C language. You can use native go language like otto

    https://github.com/robertkrimen/otto

    https://github.com/mattn/anko

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

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题