avatarKarina

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>

Minimalism and How You Can Work Less by Applying It

Photo by Arctic Qu on Unsplash

People are always talking about an education or a job they would like to have but don’t have money for it.

I’m convinced that if you want something really bad, you will find a way to do it. Because if you really want something, you will do everything in your power to achieve it.

That’s why today I want to write about how you can change your lifestyle to achieve something you would like to, but don’t have the money or time for it.

One person doesn’t necessarily need a full-time job to be able to afford life. A lot of people nowadays are working to be able to afford consumerism or a hobby. But do we really need so much stuff in our life? In the end, all these things are totally useless because you can not take them with you in your death and they cannot protect you from it or make you happy.

But a lot of people don’t share this opinion.

Until they are lying on their deathbed, they don’t see that stuff and money isn’t the most important thing in life. But then it’s too late.

It’s a shame that so many people are wasting their life, time, and potential. And I feel bad for those children whose parents never were at home because they were working. So the children were left in school, in kindergarten, or at a nanny the whole day.

It’s clear to me that the parents worked for their children to get them a good life. But for children, it’s more important to see their parents and to get love because at that age they don’t know anything about money.

The parents and all other people need to decrease their expenses so that they have more time for their loved ones. Is the most recent 4K TV really more important than spending time with family and friends?

You could work 30 hours a week and then you will have time for family, friends and your own health. That’s what I saw too when I worked full-time, that I neglected my health and my friends because I was too tired in the evening.

That’s why I recognized that I set my priorities wrong. Health, my family, and my friends are much more important than my job which I didn’t like but got me money. But maybe I’m a special case because I never knew what I should spend my money on. I’m living really thrifty and I neglect high expenses. The only big expense I did, was to buy a Tablet-PC for around 600€, for which I searched and compared a lot of websites.

If I would really decrease my expenses, I wouldn’t have bought this Tablet-PC because I already had a laptop.

I already wrote once about tips for saving money, you can take a look here if you like.

Passive Income

Another possibility for working less without giving up your lifestyle is to have a passive income. Which means you are getting income without working anything. On the internet, you can find a lot of tips and possibilities. For example, you can write a book and sell it on Amazon, or create an online course or maybe do affiliate marketing.

Myself, I don’t have a passive income yet because I’m still thinking about my ideas. But my brother is putting his music on Youtube and by adding Google Adsense to his videos, he is getting money if a certain amount of people watched it. That’s how Youtube-Stars are making money.

Minimalism

Additionally, you can save money with minimalism by living only with the bare essentials.

You just think about it first before buying something, if this thing is worth your money and time. Because with everything you are possessing, you need time to buy it, bring it back home, clean it and sell it again. So is it really necessary to have 10 to 20 decorations in one room? Isn’t one or none enough?

Because what do you achieve by having this stuff? You have to clean the shelves and the decoration.

And is this thing really making you happy now? Or are you really thinking that this thing was worth your time and money every time you are looking at it? Relationships, memories and time together with friends and family are much more important than stuff lying on your mantelpiece.

There are different types of minimalism and you don’t have to choose the radical way. You can do this your way and at your speed. You can start with one part first and then go ahead, for example only your closet, sugar, kitchen utensils, or decoration. All at once would be too much, so just start with one area and take a look if you like reducing your stuff.

From my own experience, I know that less stuff makes you happier because the room or the closet is tidier and you’re saving time by not having so many clothing choices anymore in the morning.

Enjoy this free time by having a walk or by having a cozy board game evening with friends. I am always happy when my little brother is playing a board game with me instead of watching TV. And it’s a lot more fun.

Discover your area or a city nearby. In Vienna (where I’m living right now) I sometimes just take the underground or the tram to go somewhere and then I have a walk there and discover a new part of the city. There is so much to discover in a big city, especially old houses.

You can also get a new hobby but be cautious that you are not spending too much money which you have to earn first. A lot of people are taking a well-paying job, which they don’t like, just to be able to afford a hobby. Isn’t that really counterproductive? They work more to be able to afford a hobby, but then have less time for that hobby because of their job.

Why aren’t they making that hobby their profession? Then they are doing what they like and are getting paid for it. Isn’t that a job everybody is dreaming of? You are going to get paid for doing what you like.

In this way, you like the job and you are doing your hobby every day, additionally, you’re getting paid for it and then you have time for your loved ones.

Expenses vs. Income

To be able to work less or rather work what you like, there is a basic principle you should apply.

The expenses should be less than the income.

Expenses < Income

You should just reduce the fixed costs and the variable costs so that you don’t have to earn that much to be able to afford life.

Fixed costs could be:

  • mobile phone receipt
  • Rent
  • Electricity and water
  • Internet
  • TV

Additionally, you can also save money by reducing variable costs by buying fewer clothes, and cosmetics,…

If you want to save more, you can go grocery shopping at a discounter and you can always look at special prices and compare them. I’m not saving so much money in that area because when it comes to food I’m paying more attention to quality and sustainability. I’m almost always buying organic or local products and I’m trying to avoid plastic. But the expenses are not that high because I’m also not eating that much anyway except for chocolate ;)

Conclusion

You don’t have to work full-time to be able to afford a life. You just have to set your priorities and control your expenses. My priorities are my family and friends. And for that, I don’t need money.

What are your priorities? In which area of your life could you minimize and spend less?

Thanks for reading!

Check out my profile to continue reading or check out my Twitter or subscribe to my newsletter to get inspiration and motivation regularly into your inbox.

If you like what you are reading on Medium, then support your favorite writers by starting your membership.

Life Lessons
Minimalism
Work
Money
Recommended from ReadMedium