0

I am looking to convert part of an app I have written in golang to a dll/dylib/so and make calls to it from my core golang app.

Does anyone know of any good resources or guides on how to do this as my searches are not turning up a great deal of info (much of what I'm finding are old requests for the feature which i understand is now included)

Please note: I have seen documentation on how to do the actual build, it is more around how to organise the app and expose methods that I am missing

I would also be interested in pros/cons of this approach if anyone has any experience doing something similar

SwiftD
  • 5,769
  • 6
  • 43
  • 67
  • What is your goal here? You can create a shared object on Linux, but it has limited usefulness. – JimB Jul 14 '17 at 15:58
  • Its for a cross platform app, so I would want to be able to build so, dylib and dll. My goal would be a pluggable module system – SwiftD Jul 14 '17 at 16:00
  • Plugins are an entirely different concept. Creating a shared object is so it can be called through a C interface, and like I mentioned, it's _not_ cross platform and only works on Linux. For a "plugin" system, there is the [`plugin` package](https://golang.org/pkg/plugin/). – JimB Jul 14 '17 at 16:02
  • I was not aware of that plugin package. I will take a look, maybe I have been barking up the wrong tree. As an aside - I have heard a few people say that you can only build shared libs for linux but I think that has changed - https://stackoverflow.com/questions/40573401/building-a-dll-with-go-1-7 - am I missing something on that too? I realise you would need different builds for each system – SwiftD Jul 14 '17 at 16:06
  • That other answer points to building the dll using gcc, which probably works, it's just not yet supported via the Go toolchain, and I'm not sure what issues you might encounter doing that (https://golang.org/issue/11058 probably has more relevant info). – JimB Jul 14 '17 at 16:14
  • Thanks for the pointers @JimB - If you want to present the fact that this is not yet supported with the standard go tool chain (but may be possible via gcc) as an answer I will accept (since that really addresses the reason for there not being much in the way of documentation) – SwiftD Jul 14 '17 at 16:35

1 Answers1

1

The c-shared buildmode is currently only supported on Linux.

As seen here, Building a dll with Go 1.7, you may be able to build a DLL or shared lib using gcc, but it's not supported directly by the Go toolchain.

However, if you're looking to create a "plugin" architecture, you don't want to create a C shared library, in which each instance creates a new Go runtime. There is already a plugin package which is intended for that purpose.

JimB
  • 104,193
  • 13
  • 262
  • 255