douwu0882 2017-08-10 19:47
浏览 42
已采纳

任何惯用的生成/预处理解决方案库?

I really like Go but makes me crazy about if-err hell and when I have sync datatypes in Go code with other languages. For C/C++ I can easily deal such stuff with macros, while Go developers say the idiomatic solution for Go is code generation, but I didn't find any out-of-the-box solution.

So basically, what I need is something like

  1. Read the source, for every type usage check if it is listed in special config file. If it is, then change it with the one from config.
  2. Read the source, for every function check if it is listed in config file. If it is then, change it with the code snippet from config by template and add neccessary import if it's missing.
  3. Probably, add some polymorphism based on return values to prevent type casts.
  4. Maybe, add (err error) logic. Not sure it's a good idea.

Like this

code.go

func getConn(id platform.UUID) (res1 string, res2 platform.res) {
  res1 = driver_native_res(id)
  res2 = driver_native_res(id)
  return
}

code-gen.go

import (
  "linux"
)
func getConn(id uint64) (res1 string, res2 int32, err error) {
  res1, err = linux.GetResAsString(id)
  if err != nil {
    return
  }
  res2, err = linux.GetRes(id)
  if err != nil {
    return
  }
  return
}

I know about go AST, but seems like it's not very fast to implement such features with it. I hope there is some easier solution.

  • 写回答

1条回答 默认 最新

  • dqnek0079 2017-08-10 20:12
    关注

    As you have discovered, there are no macros and are unlikely to be. There may be generics at some point which could be helpful. In the meantime, there are a few options for code generation. You can use go generate for this:

    // add a magic comment to your go file 
    // which needs some extra generated code
    // this calls a tool called stringer to generate, 
    // but it could be yacc or some homemade tool. 
    //go:generate stringer -type=Pill
    
    // call go generate to generate the methods:
    go generate
    

    https://blog.golang.org/generate

    But it really requires you to have a tool to generate the code you want like stringer.

    Or you could just use text/template, build your own template, and run a simple tool that substitutes values into this template (from a config file, or perhaps arguments on the command line).

    mytool generate -type=thing -field=type...
    ... mytool loads a tmplate file, 
    runs it through text/template and outputs it. 
    

    This is pretty straightforward and you could easily build a custom system with it, though you'll probably want to generate once, then use the code. Here are some examples of this approach:

    http://clipperhouse.github.io/gen/

    https://github.com/fragmenta/fragmenta

    Finally, you can use tools like grpc which generate structs in multiple languages in order to ease cross-language communication, which sounds like exactly the use case you are looking for:

    https://grpc.io/docs/quickstart/go.html

    I'd look at something like grpc first.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮