I'm trying to use a C++ library from Go. I also have little experience with Go.
My C++ method looks like this:
std::vector<std::string> getAttribute(const std::string key);
I have this in my .swigcxx
file:
%include "std_string.i"
%include "std_vector.i"
%include "MyCode.h"
%template(StringVector) std::vector<std::string>;
%{
#include "MyCode.h"
%}
But Go doesn't want to let me access it. I've printed out some info via:
attribs := ipuInfo.GetAttribute("foo")
fmt.Printf("%T\n", attribs)
fmt.Printf("%+v\n", attribs)
And I get:
bar.SwigcptrStringVector
<random number - address perhaps?>
But I can't work out how to get to the individual contents of the vector. I've tried:
for index, val := range bar.GetAttribute("foo") {
}
But that gives:
cannot range over bar.GetAttribute("foo") (type interface {})
I've also tried calling a .Capacity()
method on it but it isn't declared (from https://blog.nobugware.com/post/2020/c-bindings-in-go-and-swig/). I've also tried attribs[0]
and it doesn't like that either.
Does anyone have any suggestions?
Many thanks.
It looks like it's a problem with function overloading. I had 2 getAttribute
functions with the same name and different parameters. When I rename the method so that it's unique, Capacity()
and Get()
work (although range
doesn't).
It looks like Swig tried to generate wrapper code to call the correct method, but couldn't deal with different return types