如何将名称 FUNCTION 放宽为变量

How to relax the name FUNCTION into a variable

我用 Go 编写了一个插件生成器。 repo is open.

该项目在一些临时文件中创建了一些 Go 代码,定义了一个从命令行参数中获取的函数,将该代码编译成一个插件,加载该插件,获取其中的函数,并使用参数调用它,然后打印结果。

我们希望能够处理多个函数和多个插件。例如bodyreturn x+y;的一个函数SUM,bodyreturn x*y的一个函数PROD;等等。

我不希望生成的代码总是使用 常量名称函数。生成的 .go 文件不能包含 名称在运行时给出的函数,即我的 funame 在下面的代码中? Go 语言是否有某些功能禁止这样做?

//TODO: Investigate how to relax the name FUNCTION into a variable
type Xinterface interface {
    FUNCTION(x int, y int) int
}

完整代码

package main

import (
    "fmt"
    "os"
    "os/exec"
    "path/filepath"
    "plugin"
    "reflect"
    "strconv"
    "strings"
)


//TODO: Investigate how to relax the name FUNCTION into a variable
type Xinterface interface {
    FUNCTION(x int, y int) int
}

func main() {
    //Declare good variable names and create the file for the code
    funame := os.Args[1]
    funamel := strings.ToLower(funame)
    funamet := strings.Title(funame)
    fubody := os.Args[2]
    x1, err := strconv.Atoi(os.Args[3])
    y1, err := strconv.Atoi(os.Args[4])
    filename := fmt.Sprintf("/tmp/%s.go", funame)
    f, err := os.Create(filename)
    if err != nil {
        fmt.Println(err)
        return
    }
    //Here comes the program
    strprg := fmt.Sprintf(`package main 
import (
    "fmt"
)
type %s string 
func(s %s) FUNCTION (x int, y int) int { fmt.Println("")
%s} 
var %s %s`, funamel, funamel, fubody, funamet, funamel)
    fmt.Printf("func(s %s) FUNCTION (x int, y int) int { \n", funamel)
    fmt.Printf("start of %s: x=%d, y=%d\n", funamel, x1, y1)
    l, err := f.WriteString(strprg)
    if err != nil {
        fmt.Println(err)
        f.Close()
        return
    }
    fmt.Println(l, "bytes written successfully")
    err = f.Close()
    if err != nil {
        fmt.Println(err)
        return
    }

    ex, err := os.Executable()
    if err != nil {
        panic(err)
    }
    exPath := filepath.Dir(ex)
    fmt.Println(exPath)
    fmt.Println("compiling plugin")
    cmd := exec.Command("go", "build", "-buildmode=plugin", "-o", fmt.Sprintf("%s%s%s", "/tmp/", funame, ".so"), fmt.Sprintf("%s%s%s", "/tmp/", funame, ".go"))

    out, err2 := cmd.Output()
    fmt.Println(out)

    if err2 != nil {
        fmt.Println(err2)
        return
    }
    fmt.Println("loading module")
    // load module
    // 1. open the so file to load the symbols
    plug, err := plugin.Open(fmt.Sprintf("%s%s%s", "/tmp/", funame, ".so"))
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println("looking up symbol")
    // 2. look up a symbol (an exported function or variable)
    // in this case, variable funame
    symX, err := plug.Lookup(funame)
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    fmt.Println("checking module")
    // 3. Assert that loaded symbol is of a desired type
    // in this case interface type X (defined above)
    var myvar Xinterface
    myvar, ok := symX.(Xinterface)
    if !ok {
        fmt.Println(fmt.Sprintf("unexpected type from module symbol %s", reflect.TypeOf(symX.(Xinterface))))
        os.Exit(1)
    }

    // 4. use the module

    fmt.Println(myvar.FUNCTION(x1, y1))
    fmt.Println(fmt.Sprintf("Generated code: %s", fmt.Sprintf("/tmp/%s%s", funamet , ".go") ))
    fmt.Println(fmt.Sprintf("Generated object file: %s", fmt.Sprintf("/tmp/%s%s", funamet , ".so") ))

}

用法示例:

$ go run forbasile.go SUM 'return x+y' 3 5
func(s sum) FUNCTION (x int, y int) int { 
start of sum: x=3, y=5
131 bytes written successfully
/tmp/go-build104174513/b001/exe
compiling plugin
[]
loading module
looking up symbol
checking module

8
Generated code: /tmp/SUM.go
Generated object file: /tmp/SUM.so
$ go run forbasile.go SUMSQUARE 'return x*x + y*y' 3 4
func(s sumsquare) FUNCTION (x int, y int) int { 
start of sumsquare: x=3, y=4
161 bytes written successfully
/tmp/go-build555823501/b001/exe
compiling plugin
[]
loading module
looking up symbol
checking module

25
Generated code: /tmp/SUMSQUARE.go
Generated object file: /tmp/SUMSQUARE.so

godoc

A Symbol is a pointer to a variable or function.

For example, a plugin defined as

package main

import "fmt"

func F() { fmt.Printf("Hello, number %d\n", V) }

may be loaded with the Open function and then the exported package symbols V and F can be accessed

p, err := plugin.Open("plugin_name.so")
if err != nil {
    panic(err)
}
f, err := p.Lookup("F")
if err != nil {
    panic(err)
}
f.(func())() // prints "Hello, number 7"

"F" 只是一个字符串,因此您无论如何都可以在运行时更改它的值。