【问题标题】:How to add an import statement in a Go wrapper generated by SWIG如何在 SWIG 生成的 Go 包装器中添加导入语句
【发布时间】:2026-01-31 06:35:01
【问题描述】:

我有一个由SWIGGo 语言生成的包装器。在这个包装器中,我插入了一些需要 reflect 包的 Go 代码。因此,我需要在此包装器中添加行 import "reflect"SWIG 中是否有示例说明如何执行此操作?

【问题讨论】:

    标签: go swig


    【解决方案1】:

    我想你想要的是section 23.4.10 添加额外的go代码即关于

    的部分

    如果你需要导入其他的 go 包,你可以使用 %go_import 来完成。比如……

    %go_import("fmt", _ "unusedPackage", rp "renamed/package")
    
    %insert(go_wrapper) %{
    
    func foo() {
      fmt.Println("Some string:", rp.GetString())
    }
    
    // Importing the same package twice is permitted,
    // Go code will be generated with only the first instance of the import.
    %go_import("fmt")
    
    %insert(go_wrapper) %{
    
    func bar() {
      fmt.Println("Hello world!")
    }
    
    %}
    

    【讨论】:

    • 完美运行 - 谢谢!