<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Wacław The Developer on Medium]]></title>
        <description><![CDATA[Stories by Wacław The Developer on Medium]]></description>
        <link>https://medium.com/@waclawthedev?source=rss-bdf4fdfb7e9d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Cu6AA1qenkpLyQK2PpHluQ.jpeg</url>
            <title>Stories by Wacław The Developer on Medium</title>
            <link>https://medium.com/@waclawthedev?source=rss-bdf4fdfb7e9d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 13:15:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@waclawthedev/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Beware of this dangerous bug I found in Golang (filtering characters)]]></title>
            <link>https://waclawthedev.medium.com/beware-of-this-dangerous-bug-i-found-in-golang-filtering-characters-68a9a871953e?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/68a9a871953e</guid>
            <category><![CDATA[vulnerability]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[unicode]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Mon, 19 May 2025 14:45:21 GMT</pubDate>
            <atom:updated>2025-05-19T14:45:21.377Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi! Today I will share with you some interesting <strong>bug</strong> that I found when had the need to remove <strong>invisible characters</strong> from string in go. I hope <strong>you will patch your app</strong> while waiting for fix from golang maintainers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*QU_-mv5-_TfWGRZI1oFKeg.jpeg" /></figure><h3><strong>My initial approach to filter invisible characters</strong></h3><p>To filter out characters I tried to use standard package “unicode”. It have different functions to <strong>classify runes</strong>. For example it makes life easier if you want to check if rune is letter independently from language:</p><pre>unicode.IsLetter(&#39;ś&#39;) //true</pre><p>So, I decided to use power of unicode package and write the simple code:</p><pre>func withoutInvisible(s string) string {<br> var result []rune<br> for _, char := range s {<br>  if unicode.IsPrint(char) {<br>   result = append(result, char)<br>  }<br> }<br> return string(result)<br>}</pre><p><strong>With basic</strong> case it is working:</p><pre>print(withoutInvisible(&quot;\n\t\r&quot;) == &quot;&quot;) //true</pre><p>But when I decided to find different invisible characters <strong>I faced</strong> with pretty interesting ones:</p><ul><li>U+E01EF VARIATION SELECTOR-256</li><li>U+E002E TAG FULL STOP</li><li>U+E007E TAG TILDE</li><li>U+FE00 VARIATION SELECTOR-1</li></ul><p>So, I will run that line:</p><pre>print(withoutInvisible(&quot;\uFE00&quot;) == &quot;&quot;)</pre><p>it will print “false”. Things became weird when I tried <strong>to call unicode functions</strong> directly:</p><pre>println(unicode.IsGraphic(&#39;\uFE00&#39;)) //true<br>println(unicode.IsPrint(&#39;\uFE00&#39;))   //true</pre><p>So, <strong>beware of functions descriptions</strong>:</p><blockquote>IsGraphic reports whether the rune is defined as a Graphic by Unicode.<br>IsPrint reports whether the rune is defined as printable by Go.</blockquote><p>I created issue on GitHub: <a href="https://github.com/golang/go/issues/73673">https://github.com/golang/go/issues/73673</a> to help fix that as quick as possible.</p><h3>Solution</h3><p>As solution I added new functions WithoutInvisibleChars and IsVisible to my library with useful code sugars:</p><pre>go get -u github.com/waclawthedev/gopie</pre><pre>package main<br><br>import (<br> &quot;unicode&quot;<br><br> &quot;github.com/waclawthedev/gopie&quot;<br>)<br><br>func main() {<br> println(unicode.IsGraphic(&#39;\uFE00&#39;))<br> println(gopie.IsVisible(&#39;\uFE00&#39;))<br><br> println(len(gopie.WithoutInvisibleChars(&quot;\uFE00&quot;)))<br>}<br><br>//Output:<br>//true<br>//false<br>//0</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=68a9a871953e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[If Map exists — where is a Set in Golang?]]></title>
            <link>https://waclawthedev.medium.com/if-map-exists-where-is-a-set-in-golang-96fa0a4217d3?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/96fa0a4217d3</guid>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[set]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[golang-development]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Wed, 16 Apr 2025 17:40:25 GMT</pubDate>
            <atom:updated>2025-04-16T17:40:25.093Z</atom:updated>
            <content:encoded><![CDATA[<h3>If Map exists — where is a Set in Golang?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*pUWfzANlNe2Q5mWKT3Md1Q.png" /><figcaption>Where to get Set in golang</figcaption></figure><p>If you are reading that — you already noticed or are just annoyed by the lack of Set in Go. Today I will show you a simple method on how to find Set in Go.</p><p>To have the Set — first install the library that provides functions unavailable by default in Golang (<a href="https://github.com/waclawthedev/gopie">https://github.com/waclawthedev/gopie</a>):</p><pre>go get https://github.com/waclawthedev/gopie</pre><p>Now you have the ability to create a set. Let’s take a look at the example:</p><pre>package main<br><br>import (<br> &quot;fmt&quot;<br><br> &quot;github.com/waclawthedev/gopie&quot;<br>)<br><br>func main() {<br> set := gopie.NewSet[string]()<br><br> set.Add(&quot;hello&quot;)<br> set.Add(&quot;world&quot;)<br><br> fmt.Println(set.Contains(&quot;hello&quot;)) //true<br><br> set.Remove(&quot;hello&quot;)<br><br> fmt.Println(set.Contains(&quot;hello&quot;)) //false<br>}</pre><p>It is a very simple and effective implementation because it is based on a Map with an empty structure as its value. As a result, we don’t hold placeholder values in memory:</p><pre>type threadUnsafeSet[T comparable] struct {<br> m map[T]struct{}<br>}</pre><p>By using the Set, you need to remember that just like in the case of Map — you need to remember about proper synchronization of access across the goroutines.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96fa0a4217d3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang edge case: when “defer” will never work out in your code]]></title>
            <link>https://waclawthedev.medium.com/golang-edge-case-when-defer-will-never-work-out-in-your-code-f783518679e9?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/f783518679e9</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[bug-bounty]]></category>
            <category><![CDATA[defer]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[backend]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Wed, 25 Sep 2024 07:42:52 GMT</pubDate>
            <atom:updated>2024-09-25T07:42:52.419Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LqJ306Yfm7LTj9_qDMgEwQ.jpeg" /></figure><p>Today I want to show you some code, and maybe it will save you from creating tricky bugs in your project. Everything that I will show you was tried on <strong>Go 1.23.0</strong>. Let’s take a look at the code:</p><pre>import (<br>    &quot;encoding/json&quot;<br>    &quot;log&quot;<br>)<br><br>func main() {<br>    defer func() {<br>       println(&quot;simulating cleanup process&quot;)<br>    }()<br><br>    //Getting the simple error as example<br>    data := []byte(&quot;definitely not a JSON&quot;)<br>    var result map[string]any<br>    err := json.Unmarshal(data, &amp;result)<br>    if err != nil {<br>       log.Fatal(err)<br>    }<br>}</pre><p>This code can be written to decode JSON and perform some cleanup after that. If you run that, you will figure out that “simulating cleanup process” will never appear on screen. Let’s take a look at what log.Fatal(err) does under the hood:</p><pre>// Fatal is equivalent to [Print] followed by a call to [os.Exit](1).<br>func Fatal(v ...any) {<br>    std.Output(2, fmt.Sprint(v...))<br>    os.Exit(1)<br>}</pre><p><strong>The problem is that usage of os.Exit causes the instant termination without triggering the “defer” body.</strong></p><p>The same result will be if instead of log.Fatal(err) you will call just os.Exit(1) or os.Exit(0).</p><h4>What about panic() ?</h4><p>Let’s replace log.Fatal call with panic:</p><pre>import (<br>    &quot;encoding/json&quot;<br>)<br><br>func main() {<br>    defer func() {<br>       println(&quot;simulating cleanup process&quot;)<br>    }()<br><br>    //Getting the simple error as example<br>    data := []byte(&quot;definitely not a JSON&quot;)<br>    var result map[string]any<br>    err := json.Unmarshal(data, &amp;result)<br>    if err != nil {<br>       panic(err)<br>    }<br>}</pre><p>This code will print “simulating cleanup process” as expected, so defer will be working.</p><h4>What about panic() in goroutine?</h4><p>Let’s modify the code and try to run decoding in a goroutine:</p><pre>import (<br>    &quot;encoding/json&quot;<br>    &quot;sync&quot;<br>)<br><br>func main() {<br>    defer func() {<br>       println(&quot;simulating cleanup process&quot;)<br>    }()<br><br>    var wg sync.WaitGroup<br><br>    wg.Add(1)<br>    go func() {<br>       defer wg.Done()<br>       //Getting the simple error as example<br>       data := []byte(&quot;definitely not a JSON&quot;)<br>       var result map[string]any<br>       err := json.Unmarshal(data, &amp;result)<br>       if err != nil {<br>          panic(err)<br>       }<br>    }()<br>    wg.Wait() //Waiting the goroutine ends execution<br><br>    println(&quot;Done&quot;)<br>}</pre><p>Surprise, but “simulating cleanup process” will not appear on your screen :)</p><h4>Conclusion</h4><p>The example in article is pretty simple, but in real life you can miss something important that should be done in “defer” construction. For example, you can miss flushing some buffer skipping some important crash logs.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f783518679e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Improve Your GitHub PR after Review with One Command Using GPT]]></title>
            <link>https://waclawthedev.medium.com/how-to-improve-your-github-pr-after-review-with-one-command-using-gpt-cbdb28ebe78f?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/cbdb28ebe78f</guid>
            <category><![CDATA[code-review]]></category>
            <category><![CDATA[refactoring]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Sun, 04 Feb 2024 08:15:06 GMT</pubDate>
            <atom:updated>2024-02-04T08:15:06.513Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi everybody. If you have read my posts before, you may have noticed that I primarily write about Go, but today I am going to show you how to use OpenAI APIs to refactor your code after a code review.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ToDOFoFt8i4QpRS2xfzt-Q.png" /><figcaption>Image generated by AI</figcaption></figure><h3>Example project</h3><p>I created an <a href="https://github.com/waclawthedev/demo_repo_for_gptmypr">example repository</a> to show how to perform automatic refactoring using OpenAI’s GPT. The repository contains only one script, <a href="https://github.com/waclawthedev/demo_repo_for_gptmypr/blob/main/main.py">main.py</a>, that downloads the mocked data from <a href="https://randomuser.me/">https://randomuser.me/</a>, displays the summary, and saves it in a JSON file.</p><pre>import json<br>import requests<br><br><br>def fetch_random_user():<br>    response = requests.get(&quot;https://randomuser.me/api/&quot;)<br>    return response.json()<br><br><br>def save_user_to_file(user_data, filename):<br>    with open(filename, &quot;w&quot;) as file:<br>        json.dump(user_data, file, indent=4)<br><br><br>def read_user_from_file(filename):<br>    with open(filename, &quot;r&quot;) as file:<br>        return json.load(file)<br><br><br>def print_user(user):<br>    name = user[&quot;results&quot;][0][&quot;name&quot;]<br>    print(f&quot;Name: {name[&#39;title&#39;]} {name[&#39;first&#39;]} {name[&#39;last&#39;]}&quot;)<br>    email = user[&quot;results&quot;][0][&quot;email&quot;]<br>    print(f&quot;Email: {email}&quot;)<br><br><br>def main():<br>    user_data = fetch_random_user()<br>    file_name = &quot;random_user.json&quot;<br><br>    save_user_to_file(user_data, file_name)<br>    print(f&quot;User data has been saved to {file_name}&quot;)<br><br>    saved_user_data = read_user_from_file(file_name)<br><br>    print(&quot;Here is the fetched user data:&quot;)<br>    print_user(saved_user_data)<br><br><br>if __name__ == &quot;__main__&quot;:<br>    main()</pre><h4>Let’s imagine that we have a task to add displaying of the phone too in function print_user()</h4><p>So, I am adding the changes:</p><ul><li>Add printing the phone field from JSON with mocked user data</li><li>Add the types of arguments for functions for better code reading</li></ul><p>Next, let’s open <a href="https://github.com/waclawthedev/demo_repo_for_gptmypr/pull/1">PR</a>.</p><h4>Code review</h4><p>In this code I’d like to improve these moments:</p><ul><li>Add a check on data instead of directly accessing the field because we have the risk of getting an exception if the field is not there (<a href="https://github.com/waclawthedev/demo_repo_for_gptmypr/pull/1/files#r1465316065">comment</a>)</li><li>Measure how much time a get request took (<a href="https://github.com/waclawthedev/demo_repo_for_gptmypr/pull/1#discussion_r1465501567">comment</a>)</li><li>Add exception handling for the case if a file not found in <em>read_user_from_file </em>(<a href="https://github.com/waclawthedev/demo_repo_for_gptmypr/pull/1/files#r1465312188">comment</a>)</li></ul><h3>Let’s improve the code</h3><p>Let’s install the tool first. All we need is Python (I strongly recommend using <a href="https://docs.python.org/3/library/venv.html">venv</a>) and installing gptmypr:</p><pre>pip install gptmypr</pre><p>Next, we need to configure gptmypr. To operate, we need GitHub Token to have access to your PR and OpenAI API key to use GPT models.</p><h4>Generating GitHub Token</h4><p>Let’s go to <a href="https://github.com/settings/tokens">https://github.com/settings/tokens</a> and choose the <strong>Fine-grained tokens:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/808/1*z1lnemD8zAO5frHY6ob2dw.png" /></figure><p>Enter the name for your new token and the repository you want to access (or select <em>All repositories</em>):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/980/1*WjXKehDUXfMqzKto4MN7aA.png" /></figure><p>Then, tap on <strong>Repository permissions</strong> dropdown and select these two permissions:<br>Content (read only). Gptmypr requires that to read the latest version of source code:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xpT4kYmlc7Y3grcmBXQ2aw.png" /></figure><p>And Pull requests (read and write). Write permission will be used to mark comments with reactions as already procesed:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B5Ixbhg6FLxd-63TkIlv-Q.png" /></figure><p>Finally, tap “<strong>Generate token”</strong></p><h4>Generating OpenAI API key</h4><p>Here it’s much easier: just go to <a href="https://platform.openai.com/api-keys">https://platform.openai.com/api-keys</a> and tap “<strong>Create new secret key”</strong></p><h4>Running refactoring</h4><p><em>Attention! Local uncommitted changes will be lost, so I advise to stash local changes.</em></p><p>Make sure that you are in the root directory of the project and run:</p><pre>gptmypr --pr 1</pre><p>Note that 1 is number of PR. You can find it at the end of URL: <a href="https://github.com/waclawthedev/demo_repo_for_gptmypr/pull/1">https://github.com/waclawthedev/demo_repo_for_gptmypr/pull/1</a>)</p><h4>First time configuration</h4><p>If you start gptmypr for the first time, it will ask for some configuration parameters:</p><ol><li>The GitHub Token you generated before</li><li>Reaction name to mark the comments as processed. Default <em>“eyes”</em> will look something like this under a comment after it has been procesed: 👀</li><li>The OpenAI API key you generated before</li><li>The GPT model name. I like <em>gpt-4-1106-preview</em> because it’s fast, intelligent and has a fresh knowledge base<strong>.</strong><em> </em><strong>The gpt-4–0125-preview was recently released, and OpenAI says, “the model intended to reduce cases of “laziness” where the model doesn’t complete a task”. You can try experimenting with that model instead of <em>gpt-4–1106-preview</em></strong><em>.</em></li></ol><p>Note that I write this article on Feb 2024. Maybe some better model already exists.</p><h4>Result</h4><p>Let’s see how code was refactored:</p><p>Comment “measure and print how much time get request took” was fulfilled:</p><pre>def fetch_random_user() -&gt; dict:<br>    start_time = time.time()<br>    response = requests.get(&quot;https://randomuser.me/api/&quot;)<br>    elapsed_time = time.time() - start_time<br>    print(f&quot;GET request took {elapsed_time} seconds&quot;)<br>    return response.json()</pre><p>Comment “Add exception handling for case of non-existing file” was fulfilled too:</p><pre>def read_user_from_file(filename: str) -&gt; dict:<br>    try:<br>        with open(filename, &quot;r&quot;) as file:<br>            return json.load(file)<br>    except FileNotFoundError:<br>        print(f&quot;Error: The file {filename} does not exist.&quot;)<br>        return {}</pre><p>Here is result for the comment “Avoid directly getting elements and fields from dictionary. Make it safe here and everywhere in this function. Refactor this function.”:</p><pre>def print_user(user: dict):<br>    try:<br>        results = user.get(&quot;results&quot;, [])<br>        if not results:<br>            raise ValueError(&quot;No results found&quot;)<br>        result = results[0]<br>        name = result.get(&quot;name&quot;)<br>        if not name:<br>            raise ValueError(&quot;No name found&quot;)<br>        title = name.get(&quot;title&quot;, &quot;&quot;)<br>        first = name.get(&quot;first&quot;, &quot;&quot;)<br>        last = name.get(&quot;last&quot;, &quot;&quot;)<br>        print(f&quot;Name: {title} {first} {last}&quot;)<br>        email = result.get(&quot;email&quot;, &quot;No email found&quot;)<br>        print(f&quot;Email: {email}&quot;)<br>        phone = result.get(&quot;phone&quot;, &quot;No phone found&quot;)<br>        print(f&quot;Phone: {phone}&quot;)<br>    except ValueError as e:<br>        print(f&quot;Error: {e}&quot;)</pre><p>Now, take a look at comment on GitHub:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/308/1*pjJpI2N55Nf7NCnR14N8kw.png" /></figure><p>Processed comment marked with eyes emoji, and if you try to run gptmypr again, you will see:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YwCdUIQqo1EBbTsCaAZxWg.png" /></figure><p>So, to run refactoring again, unselect ‘eyes’ reaction on the comment. Also, remember that any uncommited local changes will be rewritten:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5zLHDR-buOeUP0OnZV-uqg.png" /></figure><p>Take a note that every result after running gptmypr will be unique. Obviously, the results quality hardly depends on the GPT model. I advise using these results to get fresh ideas and speed up your code improvements, not for blindly committing changes to abranch.</p><h3>Conclusion</h3><p>I hope that method will bring you a lot of fun and new ideas for coding, but remember, that all the code will be transferred thought OpenAI API according to OpenAI’s terms of use and privacy policies. Make sure, that you authorized by code owner/client/employer to do that. Also, feel free to propose the fixes or new ideas for future releases — just open an issue <a href="https://github.com/waclawthedev/GPTmyPR/issues">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cbdb28ebe78f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[I was surprised how many bots tried to hack just created website [Experiment]]]></title>
            <link>https://waclawthedev.medium.com/i-was-surprised-how-many-bots-tried-to-hack-just-created-website-experiment-e26b3fd4c399?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/e26b3fd4c399</guid>
            <category><![CDATA[bots]]></category>
            <category><![CDATA[website]]></category>
            <category><![CDATA[vulnerability]]></category>
            <category><![CDATA[wordpress]]></category>
            <category><![CDATA[cybersecurity]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Tue, 09 May 2023 15:08:32 GMT</pubDate>
            <atom:updated>2023-05-09T15:08:32.044Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/750/1*RPY6i3etWn9fdaUoticYZQ.png" /></figure><h3>How it started</h3><p>Around 3 years ago, I decided to create a website. I wanted to do it quickly, so I bought a Wordpress template to get a good start. After a few weeks, I noticed that Google was indexing random texts and ads on the homepage. When I visited the admin panel, I realized that someone had created two admin accounts and uploaded tons of custom-made plugins to use my Wordpress instance as a trash ads platform.</p><p>I was amazed by the mechanism of how easily they hacked the fresh, latest version of Wordpress with 2FA and a kilometer-long password. After searching through the logs, I understood that it was not a human activity — it was a bot. Since the first day of publishing the webpage, I received tons of requests from different IPs trying to detect versions of templates/plugins and using existing exploits/approaches to add an admin account to Wordpress.</p><p>So, a year ago, I decided to perform an interesting experiment that is still ongoing, but I already have some interesting data to share with you :)</p><h3>What I have done</h3><p>Basically, I created a honeypot — a special website designed to catch and analyze hacker activity. I used a random HTML template from the internet and intentionally left it empty so as to push away real users. We need only bots’ activity ;)</p><p>I also pretended to have an outdated Wordpress installation to provoke bots to use their full potential in attempting to hack my “Wordpress” website. Here’s the recipe:</p><ol><li>Put &lt;meta name=&quot;generator&quot; content=&quot;WordPress 3.3.1&quot;/&gt; into the head of the homepage.</li><li>Create a fake /xmlrpc.php endpoint to catch the attention of bot scanners. Xmlrpc is a very popular entry point in Wordpress for hackers. Additionally, malicious bots will &quot;verify&quot; that our &quot;website&quot; is based on Wordpress.</li><li>Create a fake /wp-login.php page to &quot;help&quot; bots define our website as Wordpress-based.</li><li>Log all requests to have a full image of attack vectors.</li></ol><h3>12 Months later…</h3><p>I took a look at DB with logs. I was quite surprised. It has <strong>~51K requests</strong> from <strong>~7K unique hosts</strong>. There is distribution of the origins of the bots:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-Amw8FqfkuAInoIgiXNH_w.png" /></figure><p>And here we have the share of requests types</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pGW1f88IZw3dBJCfKqmGMA.png" /></figure><p>I spent some time to analyze and categorize the requests by threats:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7CkXaed3WUILSAFCsIv9Ew.png" /></figure><p>So, lets talk about each of the type with examples from my logs ;)</p><h4>Bruteforce attempt</h4><p>In this category, I included activities such as attempting to log in with different credentials, such as user:admin password:12345. Here are some examples:<br><em>…<br>POST /wp-login.php<br>POST /blog/wp-login.php<br>POST /login.php<br>POST /wp/wp-login.php<br>…</em></p><h4>Exploit attempt</h4><p>This is the more interesting part. Some of the bots detected our old version of “Wordpress” and attempted to use old vulnerabilities with existing exploits. Some of them were absolutely frustrating due to typos in file paths or even paths not related to the Wordpress world. Here are some examples:<br><em>…<br>POST vendor/phpunit/phpunit/src/Util/PHP/eval-stdin.php<br>GET remote/fgt_lang?lang=/../../../..//////////dev/cmdb/sslvpn_websession<br>GET wpindex.php?idb=https://raw.githubusercontent.com/***/ExV1<br>GET wp-admin/tool.php<br>POST phpunit/src/Util/PHP/eval-stdin.php<br>…</em></p><h4>GIT probing</h4><p>Maybe you already know that storing credentials or other confidential information in Git is a VERY bad idea.</p><p>These bots are trying to access the non-existing “.git” folder in our case to find something interesting. This attack may be performed in case of misconfiguration of the HTTP server, where any non-.php files could be fetched just like on file hosting.</p><p>For example, if you use Apache and have “Options Indexes” enabled, then your directory will be working as a file hosting service. Of course, /index.php will be working as usual, but anyone can read files, such as in the /.git directory, by simply sending a GET request. Here are some examples of requests in my case:<br><em>…<br>GET .git/HEAD<br>GET dev/.git/HEAD<br>GET opt/lampp/htdocs/.git/HEAD<br>GET .git/config<br>GET images../.git/config<br>…</em></p><h4>Leakage probing</h4><p>This attack is quite simple. Attackers are trying to find something that we forgot to move/delete or just thought that nobody would find. For example, it could be backups, SQL dumps, old configurations, and so on — anything that is not protected by the CMS or rules. Here are some examples:<br><em>…<br>GET config.json<br>GET backup<br>GET .aws/credentials<br>GET backup.sql<br>GET db_backup.sql<br>GET ***.sql.xz<br>…</em></p><h4>Shell scripts usage attempt</h4><p>This attack requires two parts: using exploits to upload a special part of code and executing different commands using this code. For example, a hacker can upload a PHP file and call it via a GET request. As arguments, they can pass a bash script/command and this PHP file will work as a proxy between the hacker and the /bin/bash interpreter. Some of them are masking the name to look like a legitimate file just for distraction. Here are some examples:<br><em>…<br>POST wp-includes/shell20211028.php<br>POST x***t-shell.php<br>POST shellv3.php<br>POST wp-js.php?phpshells<br>POST wp-admin/user/wplogin.php<br>POST ma***tshell.php<br>POST wp-admin/network/wplogin.php<br>…</em></p><h4>Simple spam attempt</h4><p>Just filling our fake contact form with different spam messages. Nothing interesting</p><h4>System characteristics probing</h4><p>I can describe it as the preparation phase before attacking. Bots are trying to detect versions of installed software, CMS, web server, etc., to understand which vectors of attack to choose. In real life, they can save a lot of time by filtering out exploits that will fail because of inappropriate use cases. Here are some examples:<br><em>…<br>GET wikindex.php<br>GET owa/auth.owa<br>GET admin/login.aspx<br>GET wp-content/plugins/nd-restaurant-reservations/readme.txt<br>GET admin/filemanager/dialog.php<br>GET wp-includes/wlwmanifest.xml<br>GET streaming/clients_live.php<br>GET phpmyadmin4.8.5/index.php<br>GET wp-content/plugins/zendrop-dropshipping-and-fulfillment/readme.txt<br>GET apis/apps/v1/namespaces/kube-system/daemonsets/<br>GET blog/?author=1<br>…</em></p><h4>Trying to use xmlrpc</h4><p>In WordPress, XML-RPC is the XML interface that was designed to automate the website. In fact, it has become the most popular entry point for attackers. It allows them to bypass 2FA, request limiters, etc. The most common way to exploit it is to brute-force passwords for defined users. I received a lot of requests on my fake xmlrpc.php, most of them trying to guess passwords or find available logins on the website. <strong>My advice: just disable xmlrpc on your website if you are not using it.</strong></p><h3>Instead of conclusion</h3><p>I hope this was interesting for you. I am now considering how to scale my research into something more than an article, maybe even creating a product in the future. For now, you can follow me on Medium where I write about Go programming and various technologies. Thank you for reading!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e26b3fd4c399" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang: Handling tens of thousands of simultaneous HTTP requests even on Raspberry Pi]]></title>
            <link>https://waclawthedev.medium.com/golang-handling-tens-of-thousands-of-simultaneous-http-requests-even-on-raspberry-pi-5115ca9b152d?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/5115ca9b152d</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[highload]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[http-request]]></category>
            <category><![CDATA[raspberry-pi]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Tue, 19 Jul 2022 13:35:37 GMT</pubDate>
            <atom:updated>2022-07-19T13:35:37.981Z</atom:updated>
            <content:encoded><![CDATA[<p>Golang: Handling tens of thousands of simultaneous HTTP requests even on Raspberry Pi</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Qlcevwx36URLYrXPC3c9Uw.png" /></figure><p>Hi! Today we will have fun with concurrent HTTP requests and how to handle a really big amounts of them with minimal RAM consumption. To make this process crazy — I will choose RaspberryPi 4B as the server and Macbook Air as the client.</p><h4>Limitations</h4><p>Some theory. HTTP works over TCP, so we have a range of TCP ports: <strong>0 to 65535</strong>. It means that we can open up to ~65k simultaneous TCP connections between two machines. Let`s introduce the goal for today: send 60k requests simultaneously (i mean that we will have active 60k HTTP sessions at the same time), do some work, and respond to the client.</p><h4>Attempt 1: Writing the server and client</h4><p>Let’s start from something simple. Let the server receive a request, simulate the waiting from the response from DB (time.Sleep(time.<em>Second</em><strong><em> </em></strong>* 120)) and respond with “Hello” string. First of all — we are interested in stats about RAM usage and current HTTP sessions count. I will use beautiful <em>uilive</em> lib to update the terminal with fresh data.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00ef3d035278748fdd226949bcb3b8b5/href">https://medium.com/media/00ef3d035278748fdd226949bcb3b8b5/href</a></iframe><p>The simple client looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a9a0143b1a911eb0c2b630e783759684/href">https://medium.com/media/a9a0143b1a911eb0c2b630e783759684/href</a></iframe><p>I disable the KeepAlive to guarantee that TCP connections will not stuck or be reused.</p><p>Also, I introduced the cooldown for every 5k requests to be sure that the process will be stable.</p><p>Let’s run the server part on RaspberryPi and the client part on MacbookAir. After 1015 received requests server said about exceded limits on <em>file descriptors:</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ujdzFtREna-OcQWPtaqoAw.png" /></figure><p>Linux has two limits: <em>hard</em> and <em>soft</em>. I will override all of them by adding the next lines to<em> /etc/security/limits.conf:</em></p><pre>* soft nofile 1000000<br>* hard nofile 1000000</pre><h4>Attempt 2: After rebooting the RaspberryPi — start the server part again and try to run the client.</h4><p>After 10233 requests we are facing the same limits on the client side:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Gkwx5V2-jsORJ9sO-6WCqg.png" /></figure><p>So, I will push that limits, but on my laptop, I prefer to make it temporarily (up to the next reboot):</p><pre>sudo sysctl -w kern.maxfiles=1000000<br>sudo sysctl -w kern.maxfilesperproc=1000000</pre><p>Additionally, it is required to execute</p><pre>ulimit -S -n 1000000</pre><p>For the current terminal session, where the client is going to be run</p><h4>Attempt 3: restarting the client part</h4><p>Oops… We have just one more problem after 16377th request:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*rPp_wqouGZ6_tqqi9LJVYw.png" /></figure><p>Some more theory, why it is happening: when you are connecting to some TCP port (:8080 in our case) — the client opens the local port too, but just for the current TCP session. The range of ports is the same: <strong>0 to 65535</strong>. In OS you have the settings to define the starting port from this range. Let’s see what settings we have in MacOS:</p><pre>sysctl -a | grep portrange</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*y6Hm-BSX_AIOiZdFRwP0zA.png" /></figure><p>As you can see — we have the actual <strong>49152–65535 range for opened TCP ports</strong>. As the result, we have just 16383 ports to be opened. Let’s push that limit. I am going to set the lowest port to 1024. I will do that temporarily (up to the next reboot):</p><pre>sudo sysctl -w net.inet.ip.portrange.first=1024<br>sudo sysctl -w net.inet.ip.portrange.hifirst=1024</pre><p><strong><em>Attention! Be careful with setting the starting port. Remember, that ports 0–1024 are reserved for the known application. Basically — 49152 is optimal value.</em></strong></p><h4>Attempt 4:</h4><p>Finally, I got the result!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*6dF2VLMzwLACtwP02h1RpA.png" /></figure><p>Amazing! Clients got their “hello” string. The server side required just <strong>1230MiB of RAM </strong>for that.</p><h3>Trying to optimize RAM usage</h3><p>Let’s try to replace the basic <strong>http</strong> package with <strong>fasthttp</strong>. This library was designed for very high loads. The most advantage is minimized usage of allocs by reusing existing resources from request to request. If you are planning to use it — you need to read about the main differences on GitHub. So, this is a modified version of server part:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/443e66fca782b64d1569432e51d8410f/href">https://medium.com/media/443e66fca782b64d1569432e51d8410f/href</a></iframe><p>After running the client with the new server part getting just fantastic results!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/458/1*KMhOHMScN-wsHCsMo-h9zw.png" /></figure><p>RaspberryPi just handled the 60K request with a consumption <strong>&lt;800MiB of RAM to hold them</strong>!</p><h3>Adding the load</h3><p>60K concurrent requests are cool, but in the real world we need to do something more than just return the string. Let’s add some dummy load. For example, our <strong>getHashOfRandomString()</strong> will take sha3 hash 10K times in the loop and return the base64 of the result.</p><h4>Where the problem can be?</h4><p>If we have 60K opened TCP sessions — our computer needs to keep these sessions. It requires some free CPU time. For this study I decided to create just one worker, that will read the channel, perform the <strong>getHashOfRandomString()</strong> and send the result to another channel (different for each HTTP session).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff58de4d87eb2212ff1fa0bf4471e6ff/href">https://medium.com/media/ff58de4d87eb2212ff1fa0bf4471e6ff/href</a></iframe><p>Let’s run and see, how active connections increase fast, and after 60K active connections — smoothly decrease, generating the responses.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/714/1*pdfVkca4s0GdYYbL-wfCeg.png" /></figure><p>So, we just created an artificial bottleneck to have the situation, where just 1 thread processes the logic and the remaining 3 handle the 60K HTTP sessions. As the result, we guarantee, that all of the request will be handled and responses returned. In the real world you can create a sophisticated workers pool to use more resources of your machine and manage the concurrency.</p><h3>Instead of conclusion</h3><p>The abilities of compilable languages are really fascinating! Just study your project needs and choose the right architecture.</p><p>So, we just created artificial bottleneck to have the situation, where just 1 thread processes the logic and remaining 3 handle the 60K HTTP sessions. As the result, we guarantee, that all of the request will be handled and responses returned. In real world you can create sophisticated workers pool to use more resources of your machine and manage the concurrency.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5115ca9b152d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Docker affects your Go app performance]]></title>
            <link>https://waclawthedev.medium.com/how-docker-affects-your-go-app-performance-c63e6d02275b?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/c63e6d02275b</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Mon, 02 May 2022 15:36:06 GMT</pubDate>
            <atom:updated>2022-05-04T11:21:22.168Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Docker affects your Go app performance on Mac OS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*-aatiLU4q6kg8OvNa8FhIQ.png" /></figure><h3>Introduction</h3><p>Unlike PHP, there is no “classical” way to deploy your Go app. In 2022 Docker / Kubernetes is a gold standard to deploy non-script-based web apps. Also, some people prefer to build and deploy Go apps on bare metal (for example by using the Supervisor). Let’s figure out, is there a big difference between the performance of the app that runs on bare metal and the same app that runs as a Docker container.</p><h3><strong>Writing the benchmark</strong></h3><p>I think, that the best way to simulate the activity of the app is to execute something that uses <strong>memory</strong>, <strong>cpu,</strong> and <strong>filesystem</strong>. I decided to write a piece of code that: reads the big file (around 1GB) of JSON with complex structure, and decode this JSON. Let the structure will be:</p><pre><strong>type </strong>Country <strong>struct </strong>{<br>   Name   string  <strong>`json:&quot;name&quot;`<br>   </strong>Code   string  <strong>`json:&quot;code&quot;`<br>   </strong>States []State <strong>`json:&quot;states&quot;`<br></strong>}<br><br><strong>type </strong>State <strong>struct </strong>{<br>   Name       string <strong>`json:&quot;name&quot;`<br>   </strong>Code       string <strong>`json:&quot;code&quot;`<br>   </strong>Population int    <strong>`json:&quot;population&quot;`<br>   </strong>Cities     []City <strong>`json:&quot;cities&quot;`<br></strong>}<br><br><strong>type </strong>City <strong>struct </strong>{<br>   Name       string    <strong>`json:&quot;name&quot;`<br>   </strong>Population int       <strong>`json:&quot;population&quot;`<br>   </strong>Companies  []Company <strong>`json:&quot;companies&quot;`<br></strong>}<br><br><strong>type </strong>Company <strong>struct </strong>{<br>   Name        string     <strong>`json:&quot;name&quot;`<br>   </strong>Description string     <strong>`json:&quot;description&quot;`<br>   </strong>Employees   []Employee <strong>`json:&quot;employees&quot;`<br></strong>}<br><br><strong>type </strong>Employee <strong>struct </strong>{<br>   FirstName   string <strong>`json:&quot;first_name&quot;`<br>   </strong>LastName    string <strong>`json:&quot;last_name&quot;`<br>   </strong>PhoneNumber string <strong>`json:&quot;phone_number&quot;`<br>   </strong>BirthYear   int    <strong>`json:&quot;birth_year&quot;`<br>   </strong>BirthMonth  int    <strong>`json:&quot;birth_month&quot;`<br>   </strong>BirthDay    int    <strong>`json:&quot;birth_day&quot;`<br>   </strong>PostalCode  string <strong>`json:&quot;postal_code&quot;`<br>   </strong>Street      string <strong>`json:&quot;street&quot;`<br>   </strong>Building    int    <strong>`json:&quot;building&quot;`<br>   </strong>Resume      string <strong>`json:&quot;resume&quot;`<br></strong>}</pre><p>And the code that will decode the file:</p><pre>startTime := time.Now()<br>jsonData, err := os.ReadFile(<strong>&quot;data.bin&quot;</strong>)<br><strong>if </strong>err != nil {<br>   panic(err)<br>}<br><br><strong>var </strong>countries []Country<br>err = json.Unmarshal(jsonData, &amp;countries)<br><strong>if </strong>err != nil {<br>   panic(err)<br>}<br>fmt.Printf(<strong>&quot;Parsing complete in: %v\n&quot;</strong>, time.Since(startTime))<br><br><strong>var </strong>m runtime.MemStats<br>runtime.ReadMemStats(&amp;m)<br>fmt.Printf(<strong>&quot;Alloc = %v MiB\n&quot;</strong>, m.Alloc/1024/1024)<br>fmt.Printf(<strong>&quot;TotalAlloc = %v MiB\n&quot;</strong>, m.TotalAlloc/1024/1024)<br>fmt.Printf(<strong>&quot;Sys = %v MiB\n&quot;</strong>, m.Sys/1024/1024)<br>fmt.Printf(<strong>&quot;NumGC = %v\n&quot;</strong>, m.NumGC)</pre><h3>Let’s run!</h3><p>I will use the <strong>go 1.18.1, docker engine 20.10.14, Mac OS 12.3.1 (Intel)</strong></p><p>First of all, i will build the main.go to test the bare-metal version. In the next step, I will dockerize the app by adding the Dockerfile and building the image.</p><p>So, I am aware of caching of file with json. To prevent this, I will run my app in the next order:</p><ul><li>bare metal version</li><li>dockerized version</li><li>bare metal version</li><li>dockerized version</li><li>bare metal version</li><li>dockerized version</li><li>….</li></ul><p>One start of the app consumes around 2.5GB of RAM, so every start will prevent the caching in long term. Let’s run every version of the app <strong>30 times</strong> to ensure that the results can be interpreted correctly.</p><h3>The results</h3><p>After the loop is completed, we can see the result of 30 runs. On Y-axis is the time in second spent to parse ~1GB JSON. The unstable results at the start were caused by parallel activity on the laptop. After the start, I left my laptop alone to prevent the background activity.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-gwCDFQyeq8FWC9LZMM2Xg.png" /><figcaption>Time spend (in seconds) to parse 1GB JSON. App was run 30 times for every version.</figcaption></figure><p>The difference you can see on the chart. Next, I will get just the second part of the measures (15–30th iteration) and calculate the difference (in percents) between performance of the bare-metal app and dockerized version:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tzPFRiDr6EuM_dxe0pAJeg.png" /><figcaption>The difference between performance of dockerized and bare-metal app (percentage)</figcaption></figure><p>The text version:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/256/1*3ik_xyE23EQ34AmB5X57IQ.png" /></figure><h3>The conclusion:</h3><p>After the calculation of average difference in performance, I got the result: the <strong>dockerized Go app is 25.09% slower</strong> than the app run on bare metal. I think that Docker provides us a universal and convenient way to run and manage apps. More than that, most of apps spend most of time for waiting for DB and I/O operations. For these things, the Docker fits very well. Choose your way to deploy the app 😉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c63e6d02275b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang trap: how to copy structs properly]]></title>
            <link>https://waclawthedev.medium.com/golang-trap-how-to-copy-structs-properly-9cb2dd4c0832?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/9cb2dd4c0832</guid>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Thu, 28 Apr 2022 12:32:45 GMT</pubDate>
            <atom:updated>2022-04-28T12:32:45.568Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/580/1*PyxGsazgaV1gtqoebnrMFg.png" /></figure><h3>Instead of introduction</h3><p>Take a look at that code carefully:</p><pre><strong>package </strong>main<br><br><strong>import &quot;fmt&quot;<br><br>type </strong>car <strong>struct </strong>{<br>   model  string<br>   owners []string<br>}<br><br><strong>func </strong>main() {<br>   c := car{<br>      model:  <strong>&quot;BMW&quot;</strong>,<br>      owners: []string{<strong>&quot;John&quot;</strong>, <strong>&quot;Martha&quot;</strong>},<br>   }<br>   cCopy := c<br><br>   fmt.Printf(<strong>&quot;%+v\n&quot;</strong>, cCopy)<br>   c.owners[0] = <strong>&quot;Antony&quot;   </strong><em>//Change the original c.owners<br>   </em>c.model = <strong>&quot;Audi&quot;         </strong><em>//Change the original c.model<br>   </em>fmt.Printf(<strong>&quot;%+v&quot;</strong>, cCopy) <em>//Print the copied struct again<br></em>}</pre><p><strong>Try to run that. The output will be:</strong></p><p>{model:BMW owners:[John Martha]}<br>{model:BMW owners:[Antony Martha]}</p><h3>Why the changing of the original struct affects the copied struct?</h3><p>The short answer: because <strong>c.owners is a reference to data, not the data itself.</strong> So, the <strong>s.model</strong> will be copied by cCopy := c, but <strong>c.owners </strong>not. In the same way, the pointers will not be copied too.</p><h3>The correct solution to copy such struct:</h3><pre><strong>package </strong>main<br><br><strong>import &quot;fmt&quot;<br><br>type </strong>car <strong>struct </strong>{<br>   model  string<br>   owners []string<br>}<br><br><strong>func </strong>main() {<br>   c := car{<br>      model:  <strong>&quot;BMW&quot;</strong>,<br>      owners: []string{<strong>&quot;John&quot;</strong>, <strong>&quot;Martha&quot;</strong>},<br>   }<br>   <br>   cCopy := car{model: c.model}<br>   <strong>for </strong>_, owner := <strong>range </strong>c.owners { //Copying by loop<br>      cCopy.owners = append(cCopy.owners, owner)<br>   }<br><br>   fmt.Printf(<strong>&quot;%+v\n&quot;</strong>, cCopy)<br>   c.owners[0] = <strong>&quot;Antony&quot;</strong><em><br>   </em>c.model = <strong>&quot;Audi&quot;</strong><em><br>   </em>fmt.Printf(<strong>&quot;%+v&quot;</strong>, cCopy)<em><br></em>}</pre><p>To make your code nicer, replace the loop</p><pre><strong>for </strong>_, owner := <strong>range </strong>c.owners { //Copying by loop<br>      cCopy.owners = append(cCopy.owners, owner)<br>}</pre><p>with</p><pre>cCopy.owners = append(cCopy.owners, c.owners...)</pre><p><strong>Finally, the output will be:</strong></p><p>{model:BMW owners:[John Martha]}<br>{model:BMW owners:[John Martha]}</p><h3>If your struct contains the pointer</h3><p>I changed the example to show what we need to do with pointers in the structure. Let’s run this example</p><pre><strong>package </strong>main<br><br><strong>import &quot;fmt&quot;<br><br>type </strong>car <strong>struct </strong>{<br>   model string<br>   owner *string<br>}<br><br><strong>func </strong>main() {<br>   c := car{<br>      model: <strong>&quot;BMW&quot;</strong>,<br>      owner: getStrPtr(<strong>&quot;John&quot;</strong>),<br>   }<br><br>   cCopy := c<br><br>   fmt.Printf(<strong>&quot;%s owner&#39;s name: %s\n&quot;</strong>, cCopy.model, *cCopy.owner)<br>   *c.owner = <strong>&quot;Antony&quot;<br>   </strong>c.model = <strong>&quot;Audi&quot;<br>   </strong>fmt.Printf(<strong>&quot;%s owner&#39;s name: %s&quot;</strong>, cCopy.model, *cCopy.owner)<br>}<br><br><strong>func </strong>getStrPtr(s string) *string {<br>   <strong>return </strong>&amp;s<br>}</pre><p>As you can see from the output — the changed value of the original pointer also changed in cCopy.owner, because c.owner contains just the address in memory, not the actual data:</p><pre>BMW owner&#39;s name: John<br>BMW owner&#39;s name: Antony</pre><h3>How to copy the pointers:</h3><p>The easiest way to copy the pointer is to copy the data of the pointer to the destination pointer</p><pre><strong>package </strong>main<br><br><strong>import &quot;fmt&quot;<br><br>type </strong>car <strong>struct </strong>{<br>   model string<br>   owner *string<br>}<br><br><strong>func </strong>main() {<br>   c := car{<br>      model: <strong>&quot;BMW&quot;</strong>,<br>      owner: getStrPtr(<strong>&quot;John&quot;</strong>),<br>   }<br><br>   cCopy := c<br>   cCopy.owner = new(string) <em>//Create new pointer replacing original<br>   </em>*cCopy.owner = *c.owner   <em>//Copying by accessing to data<br><br>   </em>fmt.Printf(<strong>&quot;%s owner&#39;s name: %s\n&quot;</strong>, cCopy.model, *cCopy.owner)<br>   *c.owner = <strong>&quot;Antony&quot;<br>   </strong>c.model = <strong>&quot;Audi&quot;<br>   </strong>fmt.Printf(<strong>&quot;%s owner&#39;s name: %s&quot;</strong>, cCopy.model, *cCopy.owner)<br>}<br><br><strong>func </strong>getStrPtr(s string) *string {<br>   <strong>return </strong>&amp;s<br>}</pre><p>The output will be:</p><pre>BMW owner&#39;s name: John<br>BMW owner&#39;s name: John</pre><h3>Conclusion</h3><p>As you can see — you need to control every data structure, which you are working with. This simple example can show, how important to understand what <strong>slice</strong> and <strong>pointer</strong> really are. Also, keep in mind, that nested structs with pointers exist. For example:</p><pre><strong>type </strong>car <strong>struct </strong>{<br>   model string<br>   owner <strong>struct</strong>{<br>      name string<br>      licenceID *string<br>   }<br>}</pre><p>So, you need to control this and the same situations by copying the data, not just addresses in memory 😉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9cb2dd4c0832" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Where are “contains”, “map”, “reduce”, etc. functions in Golang (Go)?]]></title>
            <link>https://waclawthedev.medium.com/where-are-contains-map-reduce-etc-functions-in-golang-go-1fe29b849b21?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/1fe29b849b21</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[slice]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[data]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Sun, 10 Apr 2022 13:15:23 GMT</pubDate>
            <atom:updated>2022-04-10T13:15:23.228Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/880/1*zMX6DhIPJPxR2xx9qPRhdw.jpeg" /></figure><h3>Introduction</h3><p>Many people who started their programming career from Python, C# and other languages. There are typical functions for data manipulations. For example — simple contains() function to check if the array contains an element. Programmers, who became to work with Go, were faced with a lack of helpful standard functions. Today I want to describe the situation, why such functions don’t exist in Go, and what i propose to do with that.</p><h3>Why Go (Golang) don’t have such functions</h3><h4>Lack of generics (go version ≤1.18)</h4><p>Imagine the situation, where you need to implement contains() function. I think you will use the loop and == operator. If you will try to create universal contains() function — you will need the generics with the comparable data type (that supports == operation).</p><h4>Generics are always slower (go version ≥1.18)</h4><p>Many people had a dream about generics in Go. Generics were added in go version 1.18 (15 March 2022), but using generics, you are making your code slower<em>.</em> Just google the benchmark results.</p><h4>Philosophy of Go</h4><p>So, Go was created as a simple and very fast language. The authors prefer to avoid the practice of forcing you to use slower solutions. According to this — the best way is to write the required function once, that accepts your data type. This will provide the best performance. For example:</p><pre><strong>func </strong>contains(s []string, e string) bool {<br>   <strong>for </strong>_, elem := <strong>range </strong>s {<br>      <strong>if </strong>elem == e {<br>         <strong>return <em>true<br>      </em></strong>}<br>   }<br>   <strong>return <em>false<br></em></strong>}</pre><h3>If you (still) would like to use universal functions for data manipulation:</h3><p>If your project has soft requirements in part of the performance and you are ready to use go version ≥1.18 — you can take a look at my package (<a href="https://github.com/waclawthedev/go-sugaring">https://github.com/waclawthedev/go-sugaring</a>)</p><p>It is very easy to use.</p><ol><li><em>go get github.com/waclawthedev/go-sugaring</em></li><li>call sugar.Contains() with an array of any comparable type. For now, (10.04.2022, v.1.0) package contains the next functions:</li></ol><p><strong>1. </strong>sugar.Contains - check if slice contains element<br><strong>2. </strong>sugar.IndexesOf - get the slice with occurrences (indexes) of element in slice<br><strong>3. </strong>sugar.Map - Map function. Python analog<br><strong>4. </strong>sugar.Filter - Filter function. Python analog<br><strong>5. </strong>sugar.Reduce - Reduce function. Python analog<br><strong>6. </strong>sugar.Flatten - Transforms matrix to slice<br><strong>7. </strong>sugar.Min - min element in slice<br><strong>8. </strong>sugar.Max - max element in slice<br><strong>9. </strong>sugar.Keys - get keys of map as slice<br><strong>10. </strong>sugar.Values - get values of map as slice</p><h3><strong>Instead of conclusion:</strong></h3><p>I advise you to choose the right solution for your project. If you prefer comfortable programming to maximum performance — you can use my or another package with generics-based functions or get inspiration to write your own flexible solution.</p><p>P.S. If you have questions about <em>go-sugaring </em>— feel free to <a href="https://github.com/waclawthedev/go-sugaring/issues/new">create the issue</a> on GitHub :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1fe29b849b21" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Switching from PHP to Go : top 10 differences]]></title>
            <link>https://waclawthedev.medium.com/switching-from-php-to-go-top-10-differences-87fe3a923fed?source=rss-bdf4fdfb7e9d------2</link>
            <guid isPermaLink="false">https://medium.com/p/87fe3a923fed</guid>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[php-tutorial]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Wacław The Developer]]></dc:creator>
            <pubDate>Thu, 23 Dec 2021 14:38:58 GMT</pubDate>
            <atom:updated>2021-12-23T14:38:58.361Z</atom:updated>
            <content:encoded><![CDATA[<h3>Switching from PHP to Go : top 10 differences</h3><p>Hi everybody! Go (golang) becomes more and more popular in PHP devs community. Today I want to describe top fundamental differences for newcomers and remind people who are learning Go now.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*VOazXg12R481OWur" /><figcaption>Photo by <a href="https://unsplash.com/@twinsfisch?utm_source=medium&amp;utm_medium=referral">Isabella and Zsa Fischer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Go program is not a script</h3><p>The first i need to mention is that Go is a compilable language. The result of the compilation will be binary executable. Instead of it, PHP file is the script that need to be translated and executed (php has acomplicated system of caches, opcode caches, byte code generation, etc). If we want to get an answer from webpage/api endpoint — the chain of execution will be next:</p><p>PHP script: <strong>request → Webserver → php-fcgi/fpm/module (*.php file→php engine)→ response</strong></p><p>Go app: <strong>request → Go App →response</strong></p><p>This provides very good performance in comparison with PHP (it can be it tens of times faster, you can google the benchmarks)</p><h3>You should take care of types</h3><p>PHP is dynamic typed language. Go is staticaly. So, for PHP it is ok to execute that:</p><pre>$a = 1;<br>$b = “2”;<br>print($a+$b);<br>// output will be 3</pre><p>It is because PHP automatically handles the data types.</p><p>If you are trying to do the same in Go</p><pre>a := 1<br>b := <strong>&quot;2&quot;<br></strong>println(a + b)</pre><p>The compiler will reject that code</p><pre>./main.go:6:12: invalid operation: a + b (mismatched types int and string)</pre><pre>Compilation finished with exit code 2</pre><p>Statically typed languages allow us to avoid bugs (most of IDEs will warn us that type is wrong for function or another part of code) and significantly increase the performance.</p><h3>Your vars can live after end of request handling</h3><p>If you use the scripts — you keep in mind that variables will disappear after the response to the client. But Go server will clean the variable that was created in a body of the handler. Other words, if you will assign some value to a global-accessible variable -this variable will hold this variable until Go app will be restarted or other handlers will modify that variable. For example:</p><pre><strong>var </strong>n = 0<br><br><strong>func </strong>main() {<br>   http.HandleFunc(<strong>&quot;/&quot;</strong>, CounterServer)<br>   http.ListenAndServe(<strong>&quot;:8080&quot;</strong>, nil)<br>}<br><br><strong>func </strong>CounterServer(w http.ResponseWriter, r *http.Request) {<br>   n++<br>   fmt.Fprintf(w, <strong>&quot;It was request number %d&quot;</strong>, n)<br>}</pre><p>Anyway, this example is a bad parctice, because of race condition or bottleneck in case of a using mutex. <strong>Don`t use global vars</strong></p><h3>Pointers and references</h3><p>Go has ability to operate with address in memory, not the data in memory. The address of var can be used as a <strong>pointer</strong>. For example:</p><pre><strong>func </strong>main() {<br>   <strong>var </strong>n int<br><br>   <strong>var </strong>pointerToVar *int<br>   pointerToVar = &amp;n<br><br>   n = 0<br>   incrementMyVar(pointerToVar)<br>   print(n) //output will be 1<br>}<br><br><strong>func </strong>incrementMyVar(n *int) {<br>   *n++<br>}</pre><h3>Goroutines</h3><p>PHP has some kind of multithreading since PHP8.1 — Fibers (25 Nov 2021).</p><p>In go you can call any function in asyncronious mode. To do that — just call the function like that:</p><pre><strong>func </strong>main() {<br>   <strong>go </strong>function(1)<br>   <strong>go </strong>function(2)<br>   <strong>go </strong>function(3)<br>   time.Sleep(time.<strong><em>Second</em></strong>)<br>}<br><br><strong>func </strong>function(number int) {<br>   println(number)<br>}</pre><h3>Functions may return more than one variable</h3><p>Functions in Go can return multiple values. For example:</p><pre><strong>func </strong>main() {<br>   a, b, c := getNumbers()<br>   println(a)<br>   println(b)<br>   println(c)<br>}<br><br><strong>func </strong>getNumbers() (int, int, int) {<br>   <strong>return </strong>1, 2, 3<br>}</pre><h3>Interfaces and methods are not the same things from OOP languages</h3><p>In Go we have 4basic things. Structure, Interface, Function, Method.</p><p><strong><em>Structure</em></strong> holds the data something like that:</p><pre><strong>type </strong>Person <strong>struct </strong>{<br>   name     string<br>   lastname string<br>}</pre><p><strong><em>Function</em></strong> was declared this way:</p><pre><strong>func </strong>main() {<br>   nick := Person{<br>      name:     <strong>&quot;Nick&quot;</strong>,<br>      lastname: <strong>&quot;Doe&quot;</strong>,<br>   }<br>   fmt.Println(nick)<br>}</pre><p>But <strong><em>method is the function, that will apply on structure</em></strong>:</p><pre><strong>func </strong>(p Person) PrintData() {<br>   fmt.Printf(<strong>&quot;Hello, %s %s\n&quot;</strong>, p.name, p.lastname)<br>}</pre><p>Usage:</p><pre>nick := Person{<br>   name:     <strong>&quot;Nick&quot;</strong>,<br>   lastname: <strong>&quot;Doe&quot;</strong>,<br>}<br>nick.PrintData()</pre><p>So, let’s see what <strong><em>interface</em></strong> is: interface is the set of signatures of methods, that you are required to implement to have ability to use your structure by interface name. To be clear:</p><pre><strong>func </strong>main() {<br>   <strong>var </strong>cat, parrot Pet<br>   cat = Cat{<br>      name:     <strong>&quot;Jack&quot;</strong>,<br>      tailSize: 10,<br>   }<br>   parrot = Parrot{<br>      name:  <strong>&quot;Tom&quot;</strong>,<br>      color: <strong>&quot;Green&quot;</strong>,<br>   }<br>   DescribeAll(cat, parrot)<br>}<br><br><strong>func </strong>DescribeAll(first Pet, second Pet)  {<br>   first.Describe()<br>   second.Describe()<br>}<br><br><strong>type </strong>Cat <strong>struct </strong>{<br>   name     string<br>   tailSize int<br>}<br><br><strong>type </strong>Parrot <strong>struct </strong>{<br>   name  string<br>   color string<br>}<br><br><strong>type </strong>Pet <strong>interface </strong>{<br>   Describe()<br>}<br><br><strong>func </strong>(p Cat) Describe() {<br>   fmt.Printf(<strong>&quot;It is cat with name:%s and %d tail size\n&quot;</strong>, p.name, p.name, p.tailSize)<br>}<br><br><strong>func </strong>(p Parrot) Describe() {<br>   fmt.Printf(<strong>&quot;It is parrot with name:%s and %s colored\n&quot;</strong>, p.name, p.name, p.color)<br>}</pre><h3>Error handling</h3><p>Go doesn’t have the Exceptions. We are using the checks for error after most valuable functions calls. This approach allows to provide more speed for execution and decrease side-effects. Example:</p><pre><strong>func </strong>main() {<br>   <strong>if </strong>err := sayHello(<strong>&quot;&quot;</strong>); err != nil {<br>      panic(err)<br>   }<br><br>}<br><br><strong>func </strong>sayHello(name string) error {<br>   <strong>if </strong>name == <strong>&quot;&quot; </strong>{<br>      <strong>return </strong>errors.New(<strong>&quot;the name is empty&quot;</strong>)<br>   }<br>   fmt.Printf(<strong>&quot;Hello, %s&quot;</strong>, name)<br>   <strong>return </strong>nil<br>}</pre><pre>//Output: panic: the name is empty</pre><h3>Slices and arrays</h3><p>Arrays in Go have fixed size. For example, we can create this array:</p><pre><strong>var </strong>arr = [3]int{1, 2, 3}</pre><p>But how to work with dynamic sets of data?</p><p>There is something called <strong>slice.</strong> Slice does not store data, just points to sections of array.</p><p>This slice points at range of arr (0–1 element):</p><pre><strong>var </strong>slice = arr[0:2]<br>fmt.Println(slice)<br>//Output: [1 2]</pre><p>If you want from slice to handle your data automatically — it is can be done easily without creating an array.</p><pre><strong>var </strong>slice []int<br>slice = append(slice, 1, 2, 3)<br>fmt.Println(slice)</pre><h3>Classes, Access modifiers</h3><p>As I mentioned before, Go is not object-oriented language. So, there are no such things as classes or access modifiers. More than that. I (and many people) advise you to try building the architecture that you need in your project. It is mistake to copy Laravel architecture to Go. So, you can use packages to separate logic. Packages are almost the same as Java packages and look like namespaces in PHP. To hide the variables and function (for example internal logic) — you just need to name it starting with lowercase. Overwise — it will be accessible from other packages. Example:</p><pre><strong>package </strong>printer</pre><pre>//Not visible from main.go<br><strong>const <em>message </em></strong>= <strong>&quot;hello world&quot;</strong></pre><pre><strong>//Only one thing that visible from main.go<br>func </strong>PrintMessage() {<br>   print(getMessage())<br>}</pre><pre>//Not visible from main.go<br><strong>func </strong>getMessage() string{<br>   <strong>return <em>message<br></em></strong>}</pre><h3>Conclusion:</h3><p>Go language is not a regular language. It’s simplicity and compilability allow to serve tens times more request per seconds than script languages. To understand Go deeper — I advise you to reads all of the sections in the <a href="https://go.dev/tour/welcome/1">official tour</a>. Also, be free to try <a href="https://go.dev/play/">playground</a> — a place to try your code while you will study.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87fe3a923fed" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>