<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>Golang on DizzyLand</title><link>https://ka1hung.github.io/blog/tags/golang/</link><description>Recent content in Golang on DizzyLand</description><generator>Hugo -- 0.137.1</generator><language>en-us</language><lastBuildDate>Thu, 09 Jan 2025 09:09:39 +0800</lastBuildDate><atom:link href="https://ka1hung.github.io/blog/tags/golang/index.xml" rel="self" type="application/rss+xml"/><item><title>Understanding Locks and Synchronization Mechanisms in Golang's sync Package</title><link>https://ka1hung.github.io/blog/posts/go-concurrency-locks/</link><pubDate>Thu, 09 Jan 2025 09:09:39 +0800</pubDate><guid>https://ka1hung.github.io/blog/posts/go-concurrency-locks/</guid><description>&lt;p>In Golang&amp;rsquo;s standard library &lt;code>sync&lt;/code>, &lt;code>sync.Mutex&lt;/code> is a basic mutual exclusion lock used to protect shared resources and prevent multiple goroutines from simultaneously accessing or modifying data. The &lt;code>sync&lt;/code> package provides several types of locks and synchronization mechanisms. Here&amp;rsquo;s a comprehensive overview of the main lock types and their differences:&lt;/p>
&lt;h2 id="1-syncmutex-mutual-exclusion-lock">1. sync.Mutex (Mutual Exclusion Lock)&lt;/h2>
&lt;p>&lt;code>sync.Mutex&lt;/code> is the most basic lock type used to protect critical sections, ensuring only one goroutine can enter the locked area.&lt;/p></description></item><item><title>Golang-sort</title><link>https://ka1hung.github.io/blog/posts/golang-sort/</link><pubDate>Mon, 06 Jan 2025 21:52:24 +0800</pubDate><guid>https://ka1hung.github.io/blog/posts/golang-sort/</guid><description>&lt;p>Looking for efficient ways to sort data in Go? This guide covers everything from basic sorting to advanced techniques!&lt;/p>
&lt;h2 id="-basic-sorting">🚀 Basic Sorting&lt;/h2>
&lt;p>The &lt;code>sort&lt;/code> package provides fundamental sorting capabilities.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-golang" data-lang="golang">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">package&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#f92672">import&lt;/span> (
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;fmt&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#e6db74">&amp;#34;sort&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">func&lt;/span> &lt;span style="color:#a6e22e">main&lt;/span>() {
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">//sort int
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#a6e22e">s1&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> []&lt;span style="color:#66d9ef">int&lt;/span>{&lt;span style="color:#ae81ff">8&lt;/span>, &lt;span style="color:#ae81ff">2&lt;/span>, &lt;span style="color:#ae81ff">6&lt;/span>, &lt;span style="color:#ae81ff">3&lt;/span>, &lt;span style="color:#ae81ff">1&lt;/span>, &lt;span style="color:#ae81ff">4&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Ints&lt;/span>(&lt;span style="color:#a6e22e">s1&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fmt&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#a6e22e">s1&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">//sort int reverse
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Sort&lt;/span>(&lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Reverse&lt;/span>(&lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">IntSlice&lt;/span>(&lt;span style="color:#a6e22e">s1&lt;/span>)))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fmt&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#a6e22e">s1&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">//sort string
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#a6e22e">s2&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> []&lt;span style="color:#66d9ef">string&lt;/span>{&lt;span style="color:#e6db74">&amp;#34;aaa&amp;#34;&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;bbb&amp;#34;&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;6&amp;#34;&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;3&amp;#34;&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;1&amp;#34;&lt;/span>, &lt;span style="color:#e6db74">&amp;#34;4&amp;#34;&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Strings&lt;/span>(&lt;span style="color:#a6e22e">s2&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fmt&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#a6e22e">s2&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">//sort string reverse
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Sort&lt;/span>(&lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Reverse&lt;/span>(&lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">StringSlice&lt;/span>(&lt;span style="color:#a6e22e">s2&lt;/span>)))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fmt&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#a6e22e">s2&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">//sort float64
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#a6e22e">s3&lt;/span> &lt;span style="color:#f92672">:=&lt;/span> []&lt;span style="color:#66d9ef">float64&lt;/span>{&lt;span style="color:#ae81ff">1&lt;/span>, &lt;span style="color:#ae81ff">1.2&lt;/span>, &lt;span style="color:#ae81ff">0&lt;/span>, &lt;span style="color:#f92672">-&lt;/span>&lt;span style="color:#ae81ff">1.9&lt;/span>, &lt;span style="color:#f92672">-&lt;/span>&lt;span style="color:#ae81ff">82.333&lt;/span>, &lt;span style="color:#ae81ff">99.11&lt;/span>}
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Float64s&lt;/span>(&lt;span style="color:#a6e22e">s3&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fmt&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#a6e22e">s3&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#75715e">//sort float64 reverse
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#75715e">&lt;/span> &lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Sort&lt;/span>(&lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Reverse&lt;/span>(&lt;span style="color:#a6e22e">sort&lt;/span>.&lt;span style="color:#a6e22e">Float64Slice&lt;/span>(&lt;span style="color:#a6e22e">s3&lt;/span>)))
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span> &lt;span style="color:#a6e22e">fmt&lt;/span>.&lt;span style="color:#a6e22e">Println&lt;/span>(&lt;span style="color:#a6e22e">s3&lt;/span>)
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>}
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;h2 id="-object-sorting">🎯 Object Sorting&lt;/h2>
&lt;p>How to sort a list of objects by specific fields:&lt;/p></description></item><item><title>How to hide the Console window when running a Golang compiled file</title><link>https://ka1hung.github.io/blog/posts/how-to-hide-the-console-window-when-running-a-golang-compiled-file./</link><pubDate>Wed, 20 Mar 2024 10:37:55 +0800</pubDate><guid>https://ka1hung.github.io/blog/posts/how-to-hide-the-console-window-when-running-a-golang-compiled-file./</guid><description>&lt;h2 id="-ninja-ways-to-hide-console-window-in-go">🎯 Ninja Ways to Hide Console Window in Go&lt;/h2>
&lt;p>Ever wanted to make your Go applications look more professional by hiding that pesky console window? You&amp;rsquo;re in the right place! Let&amp;rsquo;s explore some ninja techniques to achieve this. 🥷&lt;/p>
&lt;h2 id="-method-1-the-quick-strike--ldflags">🚀 Method 1: The Quick Strike (-ldflags)&lt;/h2>
&lt;p>The simplest way to hide the console window is using build flags. Think of it as a stealth mode for your app!&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-bash" data-lang="bash">&lt;span style="display:flex;">&lt;span>go build -ldflags -H&lt;span style="color:#f92672">=&lt;/span>windowsgui main.go
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Pros:&lt;/p></description></item></channel></rss>