<?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 Ian on Medium]]></title>
        <description><![CDATA[Stories by Ian on Medium]]></description>
        <link>https://medium.com/@ftn8205?source=rss-8b1c70d3ec39------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*xqzGw4-iySwiF09NHjmX0Q.jpeg</url>
            <title>Stories by Ian on Medium</title>
            <link>https://medium.com/@ftn8205?source=rss-8b1c70d3ec39------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 22:10:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ftn8205/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[軟體模式設計之旅 完課心得]]></title>
            <link>https://ftn8205.medium.com/%E8%BB%9F%E9%AB%94%E6%A8%A1%E5%BC%8F%E8%A8%AD%E8%A8%88%E4%B9%8B%E6%97%85-%E5%AE%8C%E8%AA%B2%E5%BF%83%E5%BE%97-5359d50c3a4e?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/5359d50c3a4e</guid>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Thu, 30 Jul 2026 16:48:08 GMT</pubDate>
            <atom:updated>2026-07-30T16:48:08.872Z</atom:updated>
            <content:encoded><![CDATA[<h3>課程網頁</h3><p><a href="https://waterballsa.tw/design-pattern/?&amp;utm_source=google&amp;utm_medium=demandgen&amp;utm_campaign=gads_sdp_kwds&amp;gad_source=1&amp;gad_campaignid=23376053717&amp;gbraid=0AAAABBrhATnU9ALZROL-cvzhu9Kn4KavC&amp;gclid=CjwKCAjw7KvTBhA6EiwAWnutYX0gIEcpEyuxSD-y9ShXNgSPitCxwiYo3qkuUETmxVxtsDxXMblq3BoCbUsQAvD_BwE">https://waterballsa.tw/design-pattern/?&amp;utm_source=google&amp;utm_medium=demandgen&amp;utm_campaign=gads_sdp_kwds&amp;gad_source=1&amp;gad_campaignid=23376053717&amp;gbraid=0AAAABBrhATnU9ALZROL-cvzhu9Kn4KavC&amp;gclid=CjwKCAjw7KvTBhA6EiwAWnutYX0gIEcpEyuxSD-y9ShXNgSPitCxwiYo3qkuUETmxVxtsDxXMblq3BoCbUsQAvD_BwE</a></p><h3>為什麼想上這堂課</h3><p>一開始會想上這堂課，是因為我工作上一直有一個困擾：每次看到一些很亂的 code，都會覺得「這應該可以寫得更好吧」，但又不知道到底要怎麼改。以前也有自己看過設計模式的書、YouTube 影片，大概知道每個 pattern 是什麼，可是真的遇到工作上的需求時，還是很難聯想在一起。尤其需求一大，我常常會不知道從哪開始，最後就是邊寫邊改、一直重構，改到後面連自己都覺得很亂。</p><p>那時候其實也有點猶豫要不要報名，因為我本身沒什麼物件導向的底子，看到課程作業又覺得超難，很怕自己跟不上。但後來還是想說試試看，結果真的比想像中能學得起來很多。</p><h3>轉變</h3><p>我自己覺得轉變最大的時間點，大概是在作業五那邊，那時作業是要寫一個RPG遊戲的邏輯。那是第一次開始遇到比較大型、比較複雜的需求，一開始看到題目其實還是會懷疑自己：「這真的寫得出來嗎？」以前的我，大概就會直接開始硬寫，寫到一半才發現這邊漏了、那邊沒考慮到，然後來回改很久。</p><p>但那次我第一次很明顯感覺到，自己的思考方式跟以前不一樣了。</p><p>我開始會先一句一句分析需求，把需求拆開、整理出來，再畫圖、思考結構，最後才開始寫 code。過程中很像真的有一套 SOP 可以跟著走，所以即使需求變複雜，也不會像以前那樣整個卡住。印象很深的是，做到後面突然有種「其實沒有想像中那麼難」的感覺。不是題目變簡單，而是自己終於知道怎麼拆解問題了。</p><h3>應用在工作上</h3><p>以前看到很亂的 code，我通常只會覺得「這很亂」，但不知道怎麼下手。現在會比較容易看出問題點，也會開始想到：「這裡是不是可以先重構？是不是適合某個 pattern？」甚至會先整理結構，再開始加功能，而不是像以前一樣一直硬疊功能上去。</p><p>還有一個我自己很有感的地方，是跟 AI 協作的方式完全不一樣了。</p><p>以前我跟 AI 說「幫我 refactor」，它改完之後，我常常還是覺得怪怪的，但又講不出到底哪裡怪，所以只能一直叫它重改。現在因為比較知道設計思維，也會畫 UML、知道怎麼描述需求，我可以直接跟 AI 說「幫我用某種 pattern重構」，甚至直接丟架構圖給它。結果產出的 code 品質真的差很多，也更接近我想要的東西。</p><p>我後來才發現，AI 很強沒錯，但如果自己沒有設計能力，其實也很難真正駕馭它。以前我是看到 code 不好看卻不知道怎麼辦，現在是我知道想要什麼，再請 AI 幫我一起完成。</p><h3>推薦</h3><p>如果大家跟我當初一樣，擔心自己底子不夠、怕跟不上，我其實覺得不用太擔心。課程影片講得很清楚，也會搭配很多範例，還有很多作業可以練習，並且會有code review。而且遇到問題時，不管是老師、助教，discord 群組同學都可以問，大家都會熱心回答，而且現在有AI的協助，我覺得更不用害怕。</p><p>我覺得這門課最有價值的地方，不只是學幾個 design pattern，而是真的建立了一套分析需求、拆解問題、設計程式的思維。這個改變會讓你之後不管遇到什麼需求，都比較知道該怎麼開始，下一步該怎麼走。雖然過程真的蠻花時間，但我覺得很值得。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5359d50c3a4e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[[系統設計] Designing Data-Intensive Application 筆記 (Chapter 3) — Storage and Retrieval]]></title>
            <link>https://ftn8205.medium.com/%E7%B3%BB%E7%B5%B1%E8%A8%AD%E8%A8%88-designing-data-intensive-application-%E7%AD%86%E8%A8%98-chapter-3-storage-and-retrieval-9105768a3325?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/9105768a3325</guid>
            <category><![CDATA[system-design-interview]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sat, 20 Nov 2021 09:57:26 GMT</pubDate>
            <atom:updated>2021-11-20T09:57:26.457Z</atom:updated>
            <content:encoded><![CDATA[<h3>[系統設計] Designing Data-Intensive Application 筆記 (Chapter 3) — Storage and Retrieval</h3><p>在第二章中從程序設計師的觀點探討了資料庫寫入與讀取的格式，本章則是以資料庫的觀點，探討資料庫如何存入我們的數據以及提供數據</p><h3>儲存引擎</h3><p>儲存引擎是資料庫中負責資料庫中資料的儲存與提取，與底層系統檔案進行互動的底層實作。</p><p>儲存引擎可分成兩種</p><ol><li>日誌結構(log-structured)</li><li>面向頁面(page-oriented)</li></ol><h4>log-structure</h4><p>我們可以用兩個簡單的bash function來實作出一個最簡單的資料庫</p><pre>#!/bin/bash<br>db_set () {<br>  echo &quot;$1,$2&quot; &gt;&gt; database<br>}<br>db_get () {<br>  grep &quot;^$1,&quot; database | sed -e &quot;s/^$1,//&quot; | tail -n 1<br>}<br>--------------------------------------------------------------</pre><pre># 存入資料<br>&gt; db_set 42 &#39;{&quot;name&quot;:&quot;San Francisco&quot;,&quot;attractions&quot;:[&quot;Golden Gate Bridge&quot;]}&#39;</pre><pre># 讀取資料<br>&gt; db_get 42 <br>{&quot;name&quot;:&quot;San Francisco&quot;,&quot;attractions&quot;:[&quot;Golden Gate Bridge&quot;]}</pre><p><strong>db_set</strong>做的事情，就是把key、value的值存到一個檔案中，每呼叫db_set一次就會在檔案的最後加一筆資料。<strong>db_get </strong>則是從剛剛的檔案中取出最後一行的資料。</p><p>這種實作方式就符合了log-structure的精神，也就是一個append-only的文件，但這種方式的讀取效率很差，每一次讀取都必須掃過整個文件，要是文件有n筆資料，時間複雜度就是O(n)。</p><p>為了能進行高效率的查找，可以使用索引(index)。索引是從資料衍生出來的附加結構，在寫入資料時，除了寫入資料外，還要寫入索引的資訊，會增加寫入的時間，但相反的在讀取資料時，可以藉由索引加速讀取的速度。</p><p>在log-structure中，可用以下幾種方式來實作索引</p><p><strong>Hash</strong></p><p>原理</p><p>用一個hash map把所有key的位置都記在記憶體中</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/723/1*3o80tECrMXHChQ8UY2qGSQ.png" /></figure><p>使用時機</p><p>key數量不多，value卻很常被更新的狀況 (因為記憶體資源是很貴的)</p><p>例如: key存youtube的影片網址，value存點擊數</p><p>效能優化方式</p><p>1. 壓縮 (compaction): 為了避免硬碟空間被用光，我們可以壓縮它，只留這個key最新的value即可</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/746/1*QTM8iLR5OLuDJgQfNaPrmQ.png" /></figure><p>2. 壓縮 + 合併: 有很多在硬碟中的檔案，代表有很多個hash map，查找時必須要從最新的map往回找</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*cEXplF576YsG63T6G4ilRQ.png" /></figure><p>使用hash當作索引的難點</p><ol><li>文件格式: CSV不是儲存log最好的方式，用二進位的方式更好</li><li>刪除資料: 再刪除資料時，要在檔案中加一個特殊符號 (tombstone)，在檔案合併時，才會去掉它</li><li>崩潰恢復: 資料庫重啟後，hash map會不見，所以要定期的把hash map的snapshot存起來</li><li>部分寫入紀錄: 在寫log時，資料庫壞了，就需要使用一些checksum的方式來修復或刪除部分寫入的資料</li><li>併發控制: 對於寫入的順序有嚴格的要求，所以最好的實作方式為只用一個thread來寫入，很多個thread來讀。</li><li>耗費記憶體容量: 因為需要將hash map存到記憶體中</li><li>範圍查詢效率差: 如果想找0000-9999所有的key，則必須一個一個找</li></ol><p><strong>SSTables (Sorted String Table) And LSM-Trees</strong></p><p>此種方法跟hash的差別為，它要求存入文件的順序是按照key排序的，而不是以時間排序。</p><p>因為還要排序，所以寫入時間會比較慢，但有以下優點</p><ol><li>合併檔案速度快，因為可以用merge sort，如果key重複就拿比較晚生成的segment的資料。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/752/1*p9L1XKPwNbzjerk5_aSKUA.png" /></figure><p>2. 索引可以不需要存所有的key，例如想要找handiwork這個字，而hash map裡有handbag -&gt; 102134和handsome -&gt; 104667 ，因為文件內有按照key排序過，所以handiwork一定會在handbag和 handsome之間，所以可以直接從102134開始找，範圍查詢也比較高效。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/761/1*iqOglnfngmitDdibodmxmw.png" /></figure><p>維護一個SSTTable，讓資料按照key排序的方法</p><ul><li>寫入key時，將其存到記憶體中的平衡樹 (紅黑樹、AVL樹)結構，這邊稱之為memtable</li><li>記憶體滿了的話，就以SSTTable的形式寫到硬碟中</li><li>搜尋時會先找memtable，找不到才找disk</li><li>背景會有一個process不斷的處理文件的壓縮和合併</li><li>系統突然壞掉的話，記憶體中的東西會全部消失，所以每次寫入時也必須額外寫一個log到硬碟中，讓memtable有辦法恢復，恢復後在把這個log從硬碟中刪掉</li></ul><p>LSM(Log-Structure Merge Tree) Search Engine 指的就是按照以上方式在文件中進行key的排序和壓縮，常見的應用有 Level DB, Rocks DB, Cassandra, HBase, Elastic Search, solr。</p><p>性能優化</p><ol><li>系統會維護一個bloom filter在記憶體中，讓我們知道某個key不在硬碟內，就可以不用找那麼久</li><li>size-tiered壓縮: 把比較新和小的SSTable合併到比較舊和大的SSTable</li><li>leveled壓縮: 比較舊的資料，可能會比較少被查詢，所以也會把舊的資料給合併掉，可參考 https://zhuanlan.zhihu.com/p/112574579</li></ol><p><strong>Other Index</strong></p><p>secondary index: 除了一般的primary key索引外，也可多加一個index加速搜尋，例如以下把user_id設成索引，可提高join表的效率。</p><p>但假如加的欄位不是unique的話，可以索引得值是一個list，裡面包含所有match的結果。或者是加上一個row number，讓它變成唯一。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/759/1*C7VdaI2pjFpQN5mPclerhQ.png" /></figure><p>clustered index: 索引的值通常可放兩種東西</p><ol><li>實際的文檔 (clustered index): 索引對應到的值就是我們要的結果，速度較快，但就需要較大的儲存空間。</li><li>實際文檔的位置 (nonclustered index): 我們要找的資料會放到一個heap file，索引的值是那個heap file的位置，所以找到索引的值後，還要到heap file去搜尋一次，速度會比較慢。但因為索引只存位置，占用的空間較小。</li></ol><p>折衷方案為把那些常常query的資訊的值放到索引中，稱之為 (covering index)，也就是不用再到heap file多找一次。</p><p>multi-column index: 把兩個字段組合在一起當索引 (lastname + firstname)</p><p>multi-dimension index: 地理位置 (經緯度)，RGB</p><p>full-text search and fuzzy indexes: 索引儲存offset的資訊，讓它知道錯幾個字還是可以找到資料</p><h4>page-oriented</h4><p>將磁盤切成固定大小的葉面，每個頁面的值都是允許被更新的</p><p><strong>B-tree</strong></p><p>原理</p><p>和SSTables一樣，B-tree 存資料的方式也是按照key的順序來排序，但他不像log-structure的索引，會把檔案切成不同大小的segment，而是將資料庫分成很多大小一樣的block或page，一次只能讀或寫一個page，這種設計方式跟硬體比較相容。</p><p>每一個頁面都可以用一個地址來標示，一個頁面也可以藉由地址參考到另一個頁面。一個頁面對子頁面的引用數量稱為branching factor，通常是好幾百。</p><p>Ex: 想找user_id是251的資料，先到第一個頁面(100–500)找到200和300之間的指針，再指向下一個頁面(210–290)找到250和270之間的指針，再往下個頁面找，就可以找到資料。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/739/1*heEcX-DGD2gFBH6unWPdUg.png" /></figure><p>新增資料的方式</p><p>找到相對應的頁面，把資料直接更新上去，如果頁面無法容納這個多的key，就會被拆分成兩個頁面。</p><p>Ex: 想要儲存334的key，就先找到333-335的key，更新到它們中間</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/751/1*3QQZa0NPynkqIaTIfmt4rg.png" /></figure><p>讓B-tree更可靠</p><p>因為B-tree新增資料時是用更新的方式，比起LSM樹單純用append的方式複雜，所以通常會在硬碟多維護一份資料結構，稱為WAL (write-ahead log)，它也是一個append-only的檔案，再修改B-tree前先把WAL寫下來，之後就可以用它來還原資料。</p><p>假如有很多個Thread來同時存取這個B-tree，可能會導致資料不一致，所以通常會有一個latches(lightweight lock)來保護</p><p>性能優化</p><ol><li>不maintain WAL，直接把修改的page存在另一個地方，然後創建一個新的parent來指向它</li><li>在Page中增加左右方向的指針，讓查找時不用每次都要回到Parent page，再找下來</li><li>在Page中不存入完整的key，只要key有足夠的資訊可以切分範圍即可</li></ol><p>跟LSM Tree的比較</p><ol><li>B tree的寫入，必須先寫入WAL，才寫頁面(也有可能還要再做分頁的動作)，所以速度比較慢</li><li>LSM樹的吞吐量較大，因為他是照順序寫的 (sequential write &gt; random write)，而且不像B-Tree要寫在好幾個地方。</li><li>LSM Tree可以更好的被壓縮 ，所以檔案會比B-tree小</li><li>LSM Tree 會做壓縮和合併的動作，如果有讀的請求，背景又同時在做這些動作，反應時間會變得比較慢，所以效能比較難預測</li><li>LSM Tree 硬碟的頻寬要被分配來做寫入的動作 加上 壓縮的動作，隨著資料越來越多，會耗費非常大的頻寬資源</li><li>B-tree的Key只存在在一個位置，LSM Tree則會出現在很多不同的位置，所以B-tree對於加鎖做transaction比較容易</li></ol><h4>OLTP 和 OLAP</h4><p>以更高個層次來看，存儲引擎可分為兩類</p><ol><li>OLTP (Online Transactional Processing): 主要用來查找或寫入少量資料，例如查找使用者貼文數量，更新評論等等操作</li><li>OLAP (Online Analytical Processing): 每次查找時，都會一次搜尋大量資訊，但可能只需要每個紀錄中的某幾行來做一些aggregate的動作，像是(count, sum, average)，而不是單純回覆raw data給使用者。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1013/1*IfSCayFAUax2P37alKDgpw.png" /></figure><h4>Data Warehouse</h4><p>通常會把OLAP的資料庫跟OLTP分開，因為OLAP通常只是拿來分析，不希望影響到真正使用者在使用的效能。</p><p>通常OLAP的資料庫會稱作Data Warehouse，它可以用兩種方式取得OLTP資料庫的資料</p><ol><li>定期dump數據過來</li><li>利用stream的方式，即時取得資料</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/755/1*2PshUVxc7L1Q7UCkG27XYw.png" /></figure><p>在Data Warehouse中，通常會使用星型和雪花型的schema，比較容易關聯以及分析資料。</p><p>這種模型主要會有一個中心的表稱為fact_table，這個表中包含了ㄧ些資料的屬性，例如成本，銷售價格，也包含了對其他表的引用(foreign key)，其他被對應的表則稱為維度表(dimensional table)</p><p>用這種方式的好處是，當我們需要用到某些資訊時，再去關聯維度表的資料即可。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*94gYqW6J7B2TXWC1bVxnAg.png" /></figure><p>行儲存</p><p>即使fact_table有幾百個column，但我們通常只會需要某幾行，所以以行的順序來做儲存，會比較有效率。</p><p>例如: 想要加總所有的discount_price，只要取出一行，並且加總即可。而不是把所有列的discount_price都取出來再加總。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/762/1*wzJmodjhycnH5arPQQwUVQ.png" /></figure><p>行壓縮</p><p>如果是以行儲存的話，每一行的資料都是相同性質的，而且通常每一行資料會有很多重複的值出現，就可以對它們進行一些壓縮。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/736/1*2Ddxw6pzq5N1dYd7ABJ9UA.png" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9105768a3325" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kafka 介紹 + Golang程式實作]]></title>
            <link>https://ftn8205.medium.com/kafka-%E4%BB%8B%E7%B4%B9-golang%E7%A8%8B%E5%BC%8F%E5%AF%A6%E4%BD%9C-2b108481369e?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b108481369e</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[kafka]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sat, 24 Jul 2021 03:41:18 GMT</pubDate>
            <atom:updated>2021-07-24T03:41:18.688Z</atom:updated>
            <content:encoded><![CDATA[<p>介紹kafka基本原理，以及用Golang實現一個kafka的publisher和consumer</p><h3>什麼是Kafka</h3><p>Kafka是一種分散式，基於pub/sub模式的消息隊列 (Message Queue，簡稱MQ)。</p><h4>消息隊列</h4><p>消息隊列 (Message Queue)顧名思義就是一個存放消息(Message)的佇列(Queue)，主要用來幫助兩個不同服務間的溝通。</p><p>在消息隊列的架構中通常由 Producer, Message Queue 和 Consumer所組成。</p><ul><li>生產者 (Producer): 負責生產消息的服務</li><li>消息隊列 (Message Queue): 負責存放生產者消息的佇列</li><li>消費者 (Consumer): 負責消費佇列中消息的服務</li></ul><p>消息隊列有兩種模式</p><ol><li>點對點模式: 一對一的模式，消費者主動拉取消息，收到後就把消息刪掉，<strong>一個消息只能給一個消費者使用</strong>。</li><li>發布/訂閱模式 (pub/sub): 很多個消費者訂閱某個主題，消費者會主動去拉取隊列，看有沒有想要的消息。好處是消費者可以根據自己的消費速度來拉取資料，壞處是沒辦法即時獲取消息，而且不斷輪詢也會耗費資源。在這種模式下消費者消費完不會清除消息，消息會在自己設定的時限後刪掉。</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*pqjCYOi7ZTzYooZ8wRFbQw.png" /></figure><h4>使用Message Queue的優點 (TODO)</h4><ul><li>將兩個服務解耦合，當某個服務壞掉時，不會影響整個系統</li><li>讓請求變成Async，先回response接著才做事情</li><li>緩衝request，用於生產大於消費的狀況</li><li>放到queue中可保證順序，當高併發時會很好處理</li></ul><h3>Kafka架構與專有名詞解釋 (8)</h3><h4>Kafka架構由四個組件組成</h4><ul><li>Producer (生產者): 負責發送消息到 kafka broker</li><li>Consumer (消費者): 負責消化 kafka broker 中的消息</li><li>Kafka Broker: 執行kafka程序的機器，可以將多台broker組合成一個kakfa cluster。</li><li>Zookeeper: 控制中心，負責管理及維護kafka cluster的狀態</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WrwHzWSYByN5jdCxZmgtmA.png" /><figcaption><a href="https://www.educba.com/kafka-architecture/">https://www.educba.com/kafka-architecture/</a></figcaption></figure><h4>Kafka 相關名詞</h4><ul><li>Topic: kafka會將不同種類的資料放在不同的Topic中。</li><li>Partition: <br> 1. 一個Topic可以分成很多個Partition，每個Partition會存在於不同的<br> broker，讓kafka可以做負載均衡。 <br> Ex: 生產者產生了Topic1的兩個消息，他會將第一個消息丟到Topic1的Partition 0，第二個消息就可能會丟到Topic1的Partition1中。<br><br>2. 每個Partition中又可分成leader和follower，leader負責接受與傳送消息以及複製一份資料到follower中。follower只負責備份。<br> <br>3. 不同Partition中放的資料會不一樣，在同一個Partition中資料是<strong>有序的</strong>。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/675/0*flDJMKQg3T0tzbYk" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*APWf1h1hn_bB5uNG" /></figure><ul><li>Offset: 每個Partition中都會用offset來標記目前消費者已經取到哪一個資料了，是kafka用來保證partition內順序的機制。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/742/0*Bp4v_XiGJAv9PLNt" /></figure><ul><li>Consumer group: 將很多個Consumer組合成一個Consumer Group，可以增加消費者的消費能力，但有個限制是同個Group中的Consumer不能消費同一個Partition的資料，否則會有拿取同一份資料，並且處理兩次的問題。</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Zt35hl9A9zDj5WPY" /></figure><h4>總整理</h4><ul><li>(藍箭頭) producer只會傳送到每個Partition的leader</li><li>(綠箭頭) leader會複製資料到follower中</li><li>(橘箭頭) 同一個consumer group 中的consumer，必須消費不同partition的資料</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_0Eom5_wLNFX2-_GuHRj5w.png" /><figcaption><a href="https://imicrocode.com/kafka-introduction/">https://imicrocode.com/kafka-introduction/</a></figcaption></figure><h3>Kafka常用指令</h3><h4>啟動Kafka</h4><pre>先到官網下載kafka後，進到kafka的資料夾</pre><pre># STEP1 啟動zookeeper<br>bin/zookeeper-server-start.sh config/zookeeper.properties</pre><pre># STEP2 啟動kafka server<br>bin/kafka-server-start.sh config/server.properties</pre><h4>Topic相關指令</h4><pre># 創建一個Topic<br>bin/kafka-topics.sh --create --topic [Topic_Name] --bootstrap-server localhost:9092</pre><pre># 列出所有Topic<br>bin/kafka-topics.sh --list --bootstrap-server localhost:9092</pre><pre># 某個Topic詳細資訊<br>bin/kafka-topics.sh --describe --topic [Topic_Name] --bootstrap-server localhost:9092</pre><pre>-----------------------------------------<br>--create, --list, --describe: 要對Topic執行的動作 </pre><pre>--bootstrap-server 指定了kafka cluster的位置</pre><pre>--topic 指定了Topic的名稱</pre><pre>--replication-factor 指定想要的副本數</pre><pre>--partitions 指定了分區的個數</pre><h4>Producer</h4><pre>bin/kafka-console-producer.sh --topic [Topic_Name] --bootstrap-server localhost:9092</pre><pre>&gt; 接著輸入想要傳的消息</pre><h4>Consumer</h4><pre>bin/kafka-console-consumer.sh --topic [Topic_Name] --from-beginning --bootstrap-server localhost:9092</pre><h3>用Go實作Kafka consumer和producer</h3><h4>Kafka Producer</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dabe552efec3c064d4c396f7e7e3bfeb/href">https://medium.com/media/dabe552efec3c064d4c396f7e7e3bfeb/href</a></iframe><h4>Kafka Consumer</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/002ea6df160847599e40c6fed2985bdf/href">https://medium.com/media/002ea6df160847599e40c6fed2985bdf/href</a></iframe><h4>執行步驟</h4><pre>先到官網下載kafka後，進到kafka的資料夾</pre><pre># STEP1 啟動zookeeper<br>bin/zookeeper-server-start.sh config/zookeeper.properties</pre><pre># STEP2 啟動kafka server<br>bin/kafka-server-start.sh config/server.properties</pre><pre># STEP3 創立一個名字為 quickstart-events 的topic<br>bin/kafka-topics.sh --create --topic quickstart-events --bootstrap-server localhost:9092</pre><pre># STEP4 執行 producer.go<br>go run producer.go</pre><pre># STEP5 執行 consumer.go<br>go run consumer.go</pre><p>producer.go 開始發送消息到quickstart-events 這個topic</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/484/1*NJSCxTDnkuw2tINnWz8tCQ.png" /></figure><p>consumer.go 會到quick-events 這個topic 接收消息</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*I8DvhYGuHrM6G32ByQkO3w.png" /></figure><h3>Reference</h3><ul><li><a href="https://kafka.apache.org/">https://kafka.apache.org/</a></li><li><a href="https://blog.csdn.net/tflasd1157/article/details/81985722">https://blog.csdn.net/tflasd1157/article/details/81985722</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b108481369e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang 學習筆記 — Goroutine 和 Channel]]></title>
            <link>https://ftn8205.medium.com/golang-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-goroutine-%E5%92%8C-channel-86c06e706907?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/86c06e706907</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sun, 04 Jul 2021 08:53:18 GMT</pubDate>
            <atom:updated>2021-07-04T08:53:18.492Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang 學習筆記 — Goroutine 和 Channel</h3><p>介紹一下 Golang 中 Goroutine 和 Channel 的用法</p><h3>Goroutine</h3><h4>什麼是Goroutine</h4><p>Goroutine是Go實現並發的方式，有點類似Thread的概念，不同的地方是Goroutine是我們可以人為控制的，Thread則是由作業系統控制的，而且開設一個Goroutine的成本也比開設一個Thread少很多。</p><h4>創建Goroutine的方式</h4><p>在想要執行的任務前面加上一個go即可。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f33e4e16e81970f4c1192558ebf1defa/href">https://medium.com/media/f33e4e16e81970f4c1192558ebf1defa/href</a></iframe><p>我們想要印出hello來，就必須等到執行hello的那個Goroutine完成，有以下幾種方式:</p><p>第一種是讓main function 等個幾秒，但這種方式的缺點為我們無法知道Goroutine實際做了幾秒，而是給一個固定時間，如果給太少，Goroutine會無法完成，如果給太多，又會浪費多餘的時間。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d51e1af6334b879ca9efb0d52c2d8a92/href">https://medium.com/media/d51e1af6334b879ca9efb0d52c2d8a92/href</a></iframe><p>第二種是使用sync.WaitGroup，必須為每一個Goroutine做計數，每開一個就+1，完成一個就-1，直到數字為0，才會結束main的Goroutine。</p><p>用這個方式的好處為我們可以確切的知道Goroutine結束的時間，但缺點是我們必須要為每個Goroutine手動計數，會比較複雜。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4862231b506dc6fc83a87bbb1771cbd6/href">https://medium.com/media/4862231b506dc6fc83a87bbb1771cbd6/href</a></iframe><p>第三種方式為使用Channel，這種方法是 Go 語言中等待的主要方式。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3bd725698c0c7a9afe08674b62fde6eb/href">https://medium.com/media/3bd725698c0c7a9afe08674b62fde6eb/href</a></iframe><h4>Race Condition</h4><p>當兩個Goroutine同時對同一份資源做操作時，可能會造成最終的資料有問題，就叫做race condition。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e799fa27411291120209f10020a0db24/href">https://medium.com/media/e799fa27411291120209f10020a0db24/href</a></iframe><p>要防止race condition，可以用互斥鎖 (sync.Mutex)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/10a7409e78d6c5a0c5801fcad7011238/href">https://medium.com/media/10a7409e78d6c5a0c5801fcad7011238/href</a></iframe><h3>Channel</h3><p>單純開一個Goroutine讓他執行是沒有意義的，我們必須有辦法拿到另一個Goroutine執行完的結果才可以，Channel就是一個讓Goroutine可以互相溝通的方式。</p><p>Channel 就像是一個queue，可以把資料送到裡面，也可以從裡面把資料取出來。</p><h4>Channel 基本語法</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/468f80e8f774f6dc713c58d391064326/href">https://medium.com/media/468f80e8f774f6dc713c58d391064326/href</a></iframe><h4>無緩衝通道和有緩衝通道</h4><p>在初始化通道時，可以指定通道裡可放的值的數量，如果有指定的話就是有緩衝通道，沒有指定的話就是無緩衝通道。</p><p>在無緩衝通道中，必須要先有一個值來等著接收，否則會報錯。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1ac722be8515a37104d0ca829c8d3c7/href">https://medium.com/media/e1ac722be8515a37104d0ca829c8d3c7/href</a></iframe><h4>單向通道</h4><p>可以在函數的參數中，限制某個通道只能傳入值或取出值，這種通道稱為單向通道。</p><ul><li>chan&lt;- int:只能寫入的單向通道</li><li>&lt;-chan int:只能取值的單向通道</li></ul><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/08de79d03ce94cde561c838332bd1074/href">https://medium.com/media/08de79d03ce94cde561c838332bd1074/href</a></iframe><h4>Select</h4><p>select可以讓我們處理多個Channel的發送/接收，也可以讓我們在Channel阻塞(還沒塞值進去Channel或 沒有值可以從Channel取出)時做一些其他的動作。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9691d08241bf3d8c283d4542858c8ea3/href">https://medium.com/media/9691d08241bf3d8c283d4542858c8ea3/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86c06e706907" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang學習筆記 - Struct(結構)]]></title>
            <link>https://ftn8205.medium.com/golang%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-structure-%E7%B5%90%E6%A7%8B-610fb361a311?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/610fb361a311</guid>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sat, 19 Jun 2021 07:31:00 GMT</pubDate>
            <atom:updated>2021-06-19T07:32:51.198Z</atom:updated>
            <content:encoded><![CDATA[<p>Golang Struct資料型別以及Method (方法)相關筆記</p><h3>Struct</h3><p>除了一些基本型別之外，在現實生活中，我們需要描述更複雜的資料型別，例如一個人可能就包含了年齡、名字等等資料，這時我們就可以利用structure來定義出這些更複雜的資料型別。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/66c9d8fb1e5574d35ed0fc42d9964a4e/href">https://medium.com/media/66c9d8fb1e5574d35ed0fc42d9964a4e/href</a></iframe><h3>Method(方法)</h3><p>方法是一種給特定類型使用的函數</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd9a4b6dc19f8169813d9946cfd461e7/href">https://medium.com/media/bd9a4b6dc19f8169813d9946cfd461e7/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=610fb361a311" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang學習筆記- 函數]]></title>
            <link>https://ftn8205.medium.com/golang%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%87%BD%E6%95%B8-1d921ef47d3d?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d921ef47d3d</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sat, 19 Jun 2021 02:43:02 GMT</pubDate>
            <atom:updated>2021-06-19T02:43:02.603Z</atom:updated>
            <content:encoded><![CDATA[<p>Golang函數，defer筆記</p><h3>函數</h3><p>函數的用途為減少重複程式碼，當我們發現一段程式碼不斷重複出現時，可以把它寫在函數中。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bafe84c3878121984d2e15ef15de2830/href">https://medium.com/media/bafe84c3878121984d2e15ef15de2830/href</a></iframe><h3>匿名函數</h3><p>沒有函數名的函數，可以把它存在一個變數中後續使用，或是做一次性的使用。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0c9905d49ccb471579484316c8835b4/href">https://medium.com/media/e0c9905d49ccb471579484316c8835b4/href</a></iframe><h3>defer</h3><p>使用defer關鍵字，可以讓在defer之後的指令在函數的最後才執行，一般用來釋放資源用，例如做完檔案處理後的關閉檔案，或是關閉資料庫的連線等等。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c7e4f0ae86ed045876215b8d6af1923/href">https://medium.com/media/8c7e4f0ae86ed045876215b8d6af1923/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d921ef47d3d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang學習筆記 — 其他資料型別]]></title>
            <link>https://ftn8205.medium.com/golang%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E5%85%B6%E4%BB%96%E8%B3%87%E6%96%99%E5%9E%8B%E5%88%A5-50084b25a8c7?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/50084b25a8c7</guid>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Thu, 17 Jun 2021 12:54:02 GMT</pubDate>
            <atom:updated>2021-06-17T12:54:02.260Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang學習筆記 — 其他資料型別</h3><p>Golang的其他資料型別，pointer、array、slice、map</p><h3>Pointer (指標)</h3><p>指標是一個變數，用來儲存另一個變數的記憶體位址，在Golang中指標只有兩種操作。</p><p>&amp;: 取得一個變數的記憶體位址</p><p>* : 取得某個記憶體位址的值</p><p><strong>指標宣告與初始化</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79bb7790510e7fee8865b923e9ccfc08/href">https://medium.com/media/79bb7790510e7fee8865b923e9ccfc08/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/657/1*WMAJ2C3jM9RvTDk61txdww.png" /></figure><p><strong>另一種初始化的方式 (new)</strong></p><p>new: 給基本型別申請內存用的函數，返回的是對應類型的指針 (int =&gt;*int)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43f27f6cfd38d8726a6429f635be4e36/href">https://medium.com/media/43f27f6cfd38d8726a6429f635be4e36/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/883/1*zAVQ17_jBhlIX30FHtM-rw.png" /></figure><p>make 也是用來申請一個記憶體位址，但只針對slice，map 和 channel類型的記憶體創建，返回的是該類型，因為這些類型本身就是<strong>引用類型</strong>。</p><h3>Array</h3><p>array是存放元素的容器，必須指定存放的元素類型和容量，它的容量在宣告時就已經決定了，後面不能改變，若想要動態改變容量的話，必須使用slice。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/eb25cfba3e4fe93567a58284921a78fd/href">https://medium.com/media/eb25cfba3e4fe93567a58284921a78fd/href</a></iframe><h3>Slice</h3><p>slice是一種可以用來儲存相同元素的可變長度的序列，它指向一個底層的array，每個slice有它自己的長度和容量。</p><p><strong>長度</strong>: slice中元素的個數，可以用slice的內置函數len()來取得。</p><p><strong>容量</strong>: 從slice的第一個元素到它底層array的最後一個元素，可以用slice中的內置函數cap()來取得。</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/1*Lym7IVvl4PmdKQkMLucF3g.png" /></figure><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b59648c2ff2446a5297cea2a7046bd0b/href">https://medium.com/media/b59648c2ff2446a5297cea2a7046bd0b/href</a></iframe><h3>map</h3><p>map是一種以key-value方式儲存的無序資料結構，屬於一種引用類型，他必須要初始化才能使用。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e63d12aaca55adc6a095cb3c11d4fb7c/href">https://medium.com/media/e63d12aaca55adc6a095cb3c11d4fb7c/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50084b25a8c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang 學習筆記 — Interface]]></title>
            <link>https://ftn8205.medium.com/golang-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-interface-cc5c4dec52c6?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/cc5c4dec52c6</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Mon, 14 Jun 2021 13:50:19 GMT</pubDate>
            <atom:updated>2021-06-14T13:50:19.476Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang 學習筆記 — Interface</h3><p>Golang中interface的定義與用法</p><h3>Interface(介面) 定義</h3><p>Interface是一個只有方法宣告，但沒有實作的一種<strong>資料型別</strong>，某個資料型別實作了這個介面的<strong>所有方法</strong>的話，就可以等於實作了這個介面。</p><p>當它實作了interface後，這個變數會有兩種資料型別(原先定義的型別以及實作的這個介面的型別)，一個變數同時符合多個型別就稱作 polymorphis (多型)。</p><p>可以把interface想成是一個tuple，裡面包含了兩個欄位(type, value)，在還沒初始化前，都會是nil。</p><p>如果賦值給它的話，type就會變成賦值給它的那個變數的型別，value就是那個變數的值。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00a123f0f99b6630aae78a41172f9a3e/href">https://medium.com/media/00a123f0f99b6630aae78a41172f9a3e/href</a></iframe><h3>空介面</h3><p>一個介面中沒有實作任何方法的話就叫做空介面，所以所有的型別都實作了空介面，空介面會略過Golang的型別檢查，讓程式型別應用更加靈活，但也失去了靜態型別的好處。</p><p>在空介面中，看不出這個值對應的型別與方法，必須使用斷言(assertion)後才能對這些值做處理。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b02754a82a3352ef1ee790163f7d6264/href">https://medium.com/media/b02754a82a3352ef1ee790163f7d6264/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cc5c4dec52c6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang 學習筆記 —  流程控制、迴圈]]></title>
            <link>https://ftn8205.medium.com/golang-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6-%E8%BF%B4%E5%9C%88-e19ed2573793?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/e19ed2573793</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[go]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sat, 12 Jun 2021 09:10:59 GMT</pubDate>
            <atom:updated>2021-06-12T09:10:59.063Z</atom:updated>
            <content:encoded><![CDATA[<h3>Golang 學習筆記 — 流程控制、迴圈</h3><p>Golang 流程控制以及迴圈相關語法</p><h3>流程控制</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89ece5a4b8cdf219899a493bf8fb4e89/href">https://medium.com/media/89ece5a4b8cdf219899a493bf8fb4e89/href</a></iframe><h3>迴圈</h3><p>在Golang中只有for迴圈，沒有while迴圈。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6ea94f40842d04906d01a632f57e86b/href">https://medium.com/media/a6ea94f40842d04906d01a632f57e86b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e19ed2573793" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Golang 學習筆記 -變數、常數、基本型別]]></title>
            <link>https://ftn8205.medium.com/golang-%E5%AD%B8%E7%BF%92%E7%AD%86%E8%A8%98-%E8%AE%8A%E6%95%B8-%E5%B8%B8%E6%95%B8-%E5%9F%BA%E6%9C%AC%E5%9E%8B%E5%88%A5-ab4cad68f5fb?source=rss-8b1c70d3ec39------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab4cad68f5fb</guid>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <dc:creator><![CDATA[Ian]]></dc:creator>
            <pubDate>Sat, 12 Jun 2021 06:31:56 GMT</pubDate>
            <atom:updated>2021-06-13T02:35:46.302Z</atom:updated>
            <content:encoded><![CDATA[<p>Golang變數的基本資料型別，以及宣告方式</p><h3>變數</h3><p><strong>宣告變數的方式</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/825aed75e1b45c2d6bdd92e43e30aa2e/href">https://medium.com/media/825aed75e1b45c2d6bdd92e43e30aa2e/href</a></iframe><p><strong>變數的初始化</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/22b7c5465212ce96877be69fdcf88e71/href">https://medium.com/media/22b7c5465212ce96877be69fdcf88e71/href</a></iframe><h3>資料型別</h3><p>以下為幾種Golang常見的資料型別</p><h4><strong>數值類型</strong></h4><ul><li><strong>有號整數</strong></li></ul><p>int8 (佔了八個位元的記憶體空間)、int16、int32、int (大小取決於平台，如果在32位元的作業系統就佔32個位元，64位元的平台就是佔64位元)，在宣告變數時沒有寫型別的情況下，<strong>Golang預設是用int</strong>。</p><ul><li><strong>無號整數</strong></li></ul><p>uint8、uint16、uint32、uint (大小取決於平台，如果在32位元的作業系統就佔32個位元，64位元的平台就是佔64位元)，基本上沒特別規定的情況下，都會使用有號整數，不會用無號整數。</p><ul><li><strong>byte </strong>和 <strong>rune</strong></li></ul><p>byte 為 uint8 的別名，用來特別表示字元值，類似其他語言的char，</p><p>rune 為 int32的別名，也是用來特別表示字元值。</p><p>這兩者的差別在於byte表示的是ASCII碼的字元，rune表示的是經由UTF-8編碼過後的Unicode字元(例如中文字等等字元...)，<strong>預設類型為rune。</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12c456b17888213190f240ba6037799b/href">https://medium.com/media/12c456b17888213190f240ba6037799b/href</a></iframe><h4>浮點數類型</h4><p>float32、float64，如果沒寫型別的話<strong>預設就是float64</strong>。</p><h4>布林</h4><p>bool (包含了true、false兩種值)</p><h4>字串</h4><p>string</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0fec4e1f3e929d42502c26073c388317/href">https://medium.com/media/0fec4e1f3e929d42502c26073c388317/href</a></iframe><h3>常數</h3><p>在Golang中，也可以把不會改變的值宣告成常數，宣告方法如下</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d9350fc495f552c14456785acd3ecdc/href">https://medium.com/media/2d9350fc495f552c14456785acd3ecdc/href</a></iframe><h3>iota</h3><p>在Golang中沒有所謂的列舉類型，但有一個iota的功能，可以做出很像列舉的效果。</p><p>iota只能在const中使用，出現const關鍵字時，他就會被重置為0，const中每出現一個變數就會+1。</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00b89676d035ef0c1931d9e79df96556/href">https://medium.com/media/00b89676d035ef0c1931d9e79df96556/href</a></iframe><h3>特殊規則</h3><p>在Golang中，變數必須宣告成<strong>大寫</strong>，這個變數才能被其他package使用。</p><p>例如，以下目錄結構，想要在main.go裡使用tool.go裡面的函數或變數的化，必須在tool.go中都宣告成大寫才可以。</p><pre>├── main.go<br>├── go.mod <br>└── tool    <br>   └── tool.go</pre><p><strong>go.mod</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be60cec8c3d942fb318f514899232dd6/href">https://medium.com/media/be60cec8c3d942fb318f514899232dd6/href</a></iframe><p><strong>main.go</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef99d54b37ae4cffa90549baea086e1e/href">https://medium.com/media/ef99d54b37ae4cffa90549baea086e1e/href</a></iframe><p>tool.go</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/caa4da25816015b936f5d0b25200161b/href">https://medium.com/media/caa4da25816015b936f5d0b25200161b/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab4cad68f5fb" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>