0

What tools exists today for implementing the golang package in C++? I want this to work:

import (
    "fmt"
    "zuzu" // my package
}

v := zuzu.Func(1,2,3)

and the zuzu.Func() must be written in C++ as following:

// C++ code:
int Func(int a, int b, int c) {
    return a + b + c;
}

Should I use the cgo ( https://golang.org/cmd/cgo/ ) to achieve this? As I understand, I must create a .go file which provides the declarations of the Func() and uses cgo syntax to link against my .cpp / .h files with the actual implementation?

Hard questions

Can the C++ function construct and return a non-trivial Go variables, for example the map[int]map[int]int?

Can the C++ function return a value of a variable type? Pseudo-CPP-code:

// C++ code
// This function may return map[float32] or int.
MIXED_RETURN_TYPE cpp_func(int x) {
    if ( 1 == x  ) {
        return MAP_OF_FLOATS; // go type: map[float32]
    } else {
        return 2;             // go type: int
    }
}
pavelkolodin
  • 2,859
  • 3
  • 31
  • 74
  • 1
    Possible duplicate of [How to use C++ in Go?](http://stackoverflow.com/questions/1713214/how-to-use-c-in-go) – Tinwor Jun 15 '16 at 12:37
  • 3
    Go can't link directly against C++. You either use a C interface and cgo, or use swig to create one. – JimB Jun 15 '16 at 12:42
  • To the things in the "Hard questions" section, no, I don't think there's a way exposed for C to return a `map[int]map[int]int` or `interface{}`. You might make some kind of struct on the C side and then have a Go wrapper marshal it to/from Go types, or possibly provide a Go type that wraps calls manipulating a complicated C type (but cgo call overhead may make that inadvisable). – twotwotwo Jun 15 '16 at 22:16

0 Answers0