# Reflection of an external component

**URL:** <https://forum.golangbridge.org/t/reflection-of-an-external-component/35259>\
**Category:** Getting Help\
**Created:** [May 5, 2024, 1:57pm UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259 "2024-05-05T13:57:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hultan](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/h/dbc845/32.png) [@Hultan](https://forum.golangbridge.org/u/Hultan)\
**Post date:** [May 5, 2024, 1:57pm UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/1 "2024-05-05T13:57:46Z")

</div>

I have an idea for a program I want to write, that requires some plugin functionality. I want to write it in Go, but I am not sure if it is possible.

Basically I want my main program to be able to load in a number of plugins, that are required to implement a specific interface. The plugins should be placed in a specific plugin folder.

I have never used reflection (reflect package) in Go before, and my initial search did not return any examples of how to “load” a “component” from a specific path. Is that even possible in Go?

Example in C# : [reflection - Loading DLLs at runtime in C# - Stack Overflow](https://stackoverflow.com/questions/18362368/loading-dlls-at-runtime-in-c-sharp)

```go
mainExecutable
       |
       ------- plugin folder
                    |
                    ------- plugin 1
                    |
                    ------- plugin 2
                    |
                   ...
                    ------- plugin n

```

---

<div class="post-metadata">

**Author:** ![adsr303](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/adsr303/32/15041_2.png) [@adsr303](https://forum.golangbridge.org/u/adsr303)\
**Post date:** [May 5, 2024, 4:11pm UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/2 "2024-05-05T16:11:01Z")

</div>

Go programs compile into monolithic binaries. Normally they don’t load any dynamic dependencies into the runtime, other than doing system calls or wrappers for third-party native libraries (database drivers, GUI, etc.).

Your best options would be:

1. Embed a scripting language like Lua and have users write plugins in that language. Some Go programs already do that, e.g. the [Micro editor](https://micro-editor.github.io/).
2. Build plugins as separate programs (and then they don’t even need to be implemented in Go) and interface with them in some other way – stdin/stdout, HTTP, etc. I’d look at Language Server Protocol as an example of this approach.

---

<div class="post-metadata">

**Author:** ![Hultan](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/h/dbc845/32.png) [@Hultan](https://forum.golangbridge.org/u/Hultan)\
**Post date:** [May 5, 2024, 4:15pm UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/3 "2024-05-05T16:15:14Z")

</div>

Hmm…I was afraid of that…

I guess option 2 seems like the way I will have to go then. Maybe every plugin will have to create a JSON file, or something. I will have to think about that…Thanks!

---

<div class="post-metadata">

**Author:** ![jauhararifin](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/j/a4c791/32.png) [@jauhararifin](https://forum.golangbridge.org/u/jauhararifin)\
**Post date:** [May 6, 2024, 9:35am UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/4 "2024-05-06T09:35:16Z")

</div>

Have you tried this: [plugin package - plugin - Go Packages](https://pkg.go.dev/plugin) ?

You can compile golang program to a `*.so` or `*.dll` (for windows). Here is an example:

 ![image](https://forum.golangbridge.org/uploads/default/original/2X/4/43c0f816954f8191d1e8da496f573b006862e202.png)

In the image above, I create a shared library called `plugin1.so`, and it exposes 2 symbols `V` and `F`. This can be compiled independently from the root `main.go`. In the root `main.go`, I can load it using `plugin.Open("plugin1.so")` and call the `F` function.

Notice that this happen dynamically. I compile both program independent to each other.

---

<div class="post-metadata">

**Author:** ![Hultan](https://forum.golangbridge.org/letter_avatar_proxy/v4/letter/h/dbc845/32.png) [@Hultan](https://forum.golangbridge.org/u/Hultan)\
**Post date:** [May 6, 2024, 9:57am UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/5 "2024-05-06T09:57:26Z")

</div>

That looks VERY interesting! I will definitely take a look at that…

---

<div class="post-metadata">

**Author:** ![Andrew\_Stratton](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/andrew_stratton/32/12684_2.png) [@Andrew\_Stratton](https://forum.golangbridge.org/u/Andrew_Stratton)\
**Post date:** [May 12, 2024, 9:31am UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/6 "2024-05-12T09:31:38Z")

</div>

Also check out the build (run) constraints (tags/options), e.g. like this:

```auto
//go:build full

```

I use this at the top of a `main_full.go` file that is only built (or run) when I pass in a compile option like this:

```auto
go build -o quando.exe -ldflags -H=windowsgui -tags=full .

```

The main\_full itself then references the modules that are only built/run for that tag.

You can also have multiple tag options as well.

These **aren’t** run time options after deployment (compilation) - though as a developer you can run with them (basically recompiles without creating a reusable executable).

I hope this suits your use case.

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [August 10, 2024, 9:32am UTC](https://forum.golangbridge.org/t/reflection-of-an-external-component/35259/7 "2024-08-10T09:32:22Z")

</div>

Closed on August 10, 2024, 9:32am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
