从Visual C ++访问Golang模块

Is there a way of doing this?

  1. Develop a library L1 written in Golang. L1 exports functions for C language.
  2. Build L1 and generate .lib file for Visual C++.
  3. Use L1 from Visual C++ code by calling C functions in L1.

I've never tried, and I'm using linux, but here is what I know:

According to the golang documentation you can compile go code into shared library (see go help buildmode also).

To be able to call go function from c code, go function shall be exported.

In order to compile your go code into a shared library, you need to get the go standard library into a shared one too:

go install -buildmode=shared std

This will compile all the go standard code into libstd.so (on linux, the name might change on windows).

And finally, you can use the following command to get your shared library:

go install -buildmode=shared -linkshared [packages]

The standard shared library can be found in:

GOROOT/pkg/GOOS_GOARCH_dylink/ 

and your shared library under:

GOPATH/pkg/GOOS_GOARCH_dylink/

That is for the go part.

Now, if you want to call this code from a C++ project, you'll need to create the C library that wraps the go library. You can use some tool for that (I've heard about SWIG, but never tried).

EDIT: You can do something similar with static go library, but since you did not specify the library type and you will use it from C++ code, I suppose you need a shared library.