Search code examples
goswig

How to add an import statement in a Go wrapper generated by SWIG


I have a wrapper generated by SWIG for the Go language. In this wrapper, I inserted some Go code that needs the package reflect. Consequently, I need to add the line import "reflect" in this wrapper. Is there an example that illustrates how to do this in SWIG?


Solution

  • I think what you want is in section 23.4.10 Adding additional go code namely the part about

    If you need to import other go packages, you can do this with %go_import. For example...

    %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!")
    }
    
    %}