avatarLC Lynch

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4324

Abstract

span> <span class="hljs-params">(c *calculator)</span></span> Calculate(num... <span class="hljs-type">int</span>) { ans := <span class="hljs-number">0</span> <span class="hljs-keyword">for</span> _, n := <span class="hljs-keyword">range</span> num { ans += n } fmt.Println(ans) }

<span class="hljs-keyword">var</span> Calculator calculator</pre></div><p id="c1d0">We exposed a struct which implements the method Calculate. This is required for the discovery and registration of the plugin. Now to compile this and make it a shared-object we need to build the library as a plugin.</p><div id="b6b4"><pre><span class="hljs-attribute">go build -o -buildmode</span>=plugin .</pre></div><p id="198a">After this, we will have a shared object file(in our case “<i>add.so</i>”) which we can dynamically load at runtime in our main application.</p><figure id="5129"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*HEBnzWVkvUP0JjhOsfOH7Q.png"><figcaption>updated file structure of the library</figcaption></figure><p id="5408">Now to dynamically load the library into our main application at run time we need use the plugin system of Golang.</p><div id="0960"><pre><span class="hljs-keyword">import</span> ( <span class="hljs-string">"fmt"</span> <span class="hljs-string">"os"</span> <span class="hljs-string">"plugin"</span> )

<span class="hljs-keyword">type</span> Calculator <span class="hljs-keyword">interface</span> { Calculate(num... <span class="hljs-type">int</span>) }

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> { calculatorPlugin, err := plugin.Open(<span class="hljs-string">"/abs/path/to/shared/object/file"</span>) <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> { fmt.Println(<span class="hljs-string">"error while opening shared object file"</span>) os.Exit(<span class="hljs-number">1</span>) }

symCalculator, err := calculatorPlugin.Lookup(<span class="hljs-string">"Calculator"</span>) <span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> { fmt.Println(<span class="hljs-string">"error while lookup"</span>) os.Exit(<span class="hljs-number">1</span>) }

<span class="hljs-keyword">var</span> calculator Calculator calculator, ok := symCalculator.(Calculator) <span class="hljs-keyword">if</span> !ok { fmt.Println(<span class="hljs-string">"unexpected type from module symbol"</span>) os.Exit(<span class="hljs-number">1</span>) }

calculator.Calculate(<span class="hljs-number">3</span>,<span class="hljs-number">4</span>) }</pre></div><p id="f4f9">In this case if we are making any changes to library, we don’t need to re-compile our main application. We only need to recompile the library’s shared object and rerun our main application.</p><p id="a32d">Due to the different characteristics, the advantages and disadvantages of static and dynamic libraries are also obvious; binaries that rely only on static libraries and are generated by static linking can be executed independently because they contain all the dependencies, but the compilation result is also larger. Dynamic libraries can be shared among multiple executables, which can reduce the memory footprint, and their linking process is often triggered during loading or running, so they can contain some modules that can be hot-plugged and reduce the memory footprint. Compiling binaries using static linking has very obvious deployment advantages, and the final compiled product will run directly on most machines. The deployment benefits of static linking are far more important than the lower memory footprint, that’s why Golang uses static linking as the default linking method.</p><h1 id="d290">Issues with Dynamic-linking (shared library plugins) in Go</h1><p id="91cd">Plugins using shared libraries and the plugin package work well for Golang, as the previous section demonstrates. However, this approach also has some serious downsides. The most important downside is that Golang is very picky about keeping the main application and the shared libraries it loads compatible.</p><p id="285c">As an experiment, try using different versions of a common d

Options

ependency in the plugin application and the main application, rebuild the main application and run it. Most likely you’ll get this error:</p><div id="443c"><pre><span class="hljs-comment">"plugin was built with a different version of package XXX"</span></pre></div><figure id="d928"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*ow5zqQqWHmQoZqaC.gif"><figcaption></figcaption></figure><p id="35aa">The reason for this is that Golang wants all the versions of all packages in the main application and plugins to match exactly. It’s clear what motivates this: safety.</p><p id="7b0e">Consider C and C++ as counter-examples. In these languages, an application can load a shared library with dlopen and subsequently use dlsym to obtain symbols from it. dlsym is extremely weakly typed; it takes a symbol name and returns a void*. It’s up to the user to cast this to a concrete function type. If the function type changes because of a version update, the result can very likely be some sort of segmentation fault or even memory corruption.</p><p id="80a8">Since Golang relies on shared libraries from plugins, it has the same inherent safety issues. It tries to protect programmers from shooting themselves in the foot by ensuring that the application has been built with the same versions of packages as all its plugins. This helps avoid mismatch. In addition, the version of the Golang compiler used to build the application and plugins must match exactly.</p><p id="74b9">However, this protection comes with downsides — making developing plugins somewhat cumbersome. Having to rebuild all plugins whenever any common packages change — even in ways that don’t affect the plugin interface — is a heavy burden. Especially, considering that by their very nature plugins are typically developed separately from the main application; they may live in separate repositories, have separate release cadences etc.</p><h1 id="bfc1">Alternative approaches to shared library plugins in Golang</h1><p id="c642">Given that the plugin package was only added to Go in <a href="https://golang.org/doc/go1.8">version 1.8</a> and the limitation described previously, it’s not surprising that the Go ecosystem saw the emergence of alternative plugin approaches.</p><p id="87e5">One of the innovative approaches involves plugins via RPC. In this method instead of loading the plugins into the host process we load them in a separate process which then communicates to host via RPC or just TCP on localhost. It has several important upsides:</p><ul><li>Isolation: crash in a plugin does not bring the whole application down.</li><li>Interoperability between languages: if RPC is the interface, do you care what language the plugin is written in?</li><li>Distribution: if plugins interface via the network, we can easily distribute them to run on different machines for gains in performance, reliability, and so on.</li></ul><p id="f78c">Moreover, Golang makes this particularly easy by having a fairly capable RPC package right in the standard library: net/rpc.</p><p id="4495">One of the most widely used RPC-based plugin systems is <a href="https://github.com/hashicorp/go-plugin">hashicorp/go-plugin</a>. Hashicorp is well known for creating great Golang software, and apparently, they use go-plugin for many of their systems, so it’s battle-tested (though its documentation could be better 😛)</p><p id="1a23">Golang-plugin runs on top of net/rpc but also supports gRPC. Advanced RPC protocols like gRPC are well suitable for plugins because they include versioning out-of-the-box, tackling the difficult interoperability problem between different versions of plugins vs. the main application.</p><p id="c8a4">However, this is also<b> not a perfect solution </b>as we talk about the fourth fundamental of plugin systems- “<b>Extension APIs</b>”. In a complex system, a plugin might make lot of Extension APIs calls which will end up increasing the latency if we are making network calls via RPC or TCP.</p><h1 id="1c76">Conclusion</h1><p id="03fb">Golang still has a long way to go when it comes to Dynamic-linking (shared library plugins). No solution discussed in this blog can be considered perfect. One needs to consider the pros and cons of each solution available as per their specific requirements.</p></article></body>

How Meditation Brings Out Your Best Side

Go inward to embody the highest version of yourself

Photo by John De Leon: Pexels

Many people view meditation as a way to relax. Shutting your eyes and taking a few deep breaths brings tranquility. It does, but there is so much more to this ancient practice. Its main focus is being present- bringing your full awareness into the current moment. This helps you tune into your most authentic self.

When you quiet your mind, you are able to think more clearly. Meditation gives you perspective on strong emotions and brings you more self-awareness. Connecting to your subconscious makes you more intuitive, boosts your mood, and improves your health. Even just a few minutes a day can transform your life.

Meditation Connects You to Your Subconscious

Meditation connects you to your subconscious, leaves you feeling more clear-headed and refreshed, and gives your thinking mind a break. Everyone has two mindsets- your instinctual/intuitive side and logical thought. There is a time to use both approaches and they complement each other.

You need logic to analyze a situation or decision, but most people get caught in a cycle of overthinking which leads to anxiety and stress. We all have our own internal compass that guides us to the choices that are for our best benefit. It’s just a matter of learning to listen to the gentle nudges of your intuition.

Meditation Quiets Your Mind

When you overanalyze your problems it takes you out of the present moment. Getting caught up in this coulda, shoulda, woulda syndrome takes you away from what is actually happening now. Meditation calms your mind and reduces your anxiety. This allows you to think more clearly and make better decisions.

It is natural to think about how your life could be different and worry about all the responsibilities you have coming up. Meditation makes you more productive because it improves your ability to focus on the task at hand. When you quiet your mind, you are more open to new experiences and opportunities.

“Life is available only in the present moment. If you abandon the present moment you cannot live the moments of your daily life deeply.” ~ Thich Nhat Hanh

Photo by James Wheeler: Pexels

Meditation Improves Your Reactions

Meditation taps into the instinctual side of your mind and connects you to your current self: the basis of your personal power. There is a fight between your self-confident self and your self-doubt. If you are not present it is easy to get wrapped up in these two extremes.

See life for what it is- not what it could have been or what it will be later. Enjoy what you have, because you never know how life will change. Don’t forget to give yourself the benefit of the doubt- if clearing your mind of all your worries was easy there wouldn’t be so many books and blog posts on the subject.

Meditation Makes You Happier and Healthier

Meditating regularly boosts your mood and improves your health. It lowers your blood pressure and heart rate and reduces the amount of cortisol in your body. Reducing your stress and anxiety has a profound effect on your mind, body, and spirit. Give yourself time to recharge, and you will have more energy and mental clarity.

Going inward helps you understand yourself better and work through difficult emotions. You are much happier and more vibrant when you are connected to your own thoughts and feelings. Be willing to work through your past and then let it go. Staying present teaches you to love where you are on your journey. Happiness will always find a grateful heart.

Practice

Close your eyes now, as long as you are in a safe space.

Start by noticing your breath- are your breaths shallow?

Are they relaxed? Do you feel at ease in your body?

Let each breath become longer and deeper.

Gradually letting go of your need to control the moment.

Just letting yourself be here now. To just be still.

Do you have any areas of tension?

Anywhere that feels out of alignment?

Any pain or discomfort?

Feel this now and relax into it.

Soften into these areas and feel them melt into the ground.

Seeing all the stress drain from your body.

Feel into the area under yourself.

Is this surface hard? Soft? Comfortable?

Sink through the floor and allow your body to be heavy.

Rooted in the present moment.

When meditation becomes a habit you feel more peaceful. Even just 20 minutes a day creates huge shifts in your life. Going inward connects you with your subconscious and helps you embody your most authentic self. Create a consistent practice and you will transform the way you think and act.

Enhance your intuition and you will make better decisions and think more clearly. Meditation is not only a practice, it is a way of life. Focusing on the present moment makes you happier and more resilient. Love where you are in this moment to find deep fulfillment. Becoming the best version of yourself starts with tuning into your own breath.

Meditation
Mindfulness
Dancingelephantspress
Holistic Health
Self
Recommended from ReadMedium