{"componentChunkName":"component---src-templates-blog-post-js","path":"/2020-05-26-go-starter/","result":{"data":{"site":{"siteMetadata":{"title":"Howard's Note","disqusShortname":""}},"markdownRemark":{"id":"5110fb09-8cdc-5983-bc4d-600a3e55c370","html":"<p>根據 wiki，</p>\n<blockquote>\n<p>Go是Google開發的一種靜態強型別、編譯型、並發型，並具有垃圾回收功能的程式語言。</p>\n</blockquote>\n<p>現在在雲服務中，很多人在使用golang來實作服務。因此，想試試看 寫 golang是什麼感覺，</p>\n<p>因此，隨便上網找了一個 starter turtoril 來 step by step 照著做，並且記錄一下。</p>\n<blockquote>\n<p>以下的內容參考、翻譯自 <a href=\"https://dev.to/moficodes/build-your-first-rest-api-with-go-2gcj\">https://dev.to/moficodes/build-your-first-rest-api-with-go-2gcj</a></p>\n</blockquote>\n<h1>Go 中的 Rest Api</h1>\n<p>寫restful api，為什麼要選擇 go ?</p>\n<ul>\n<li>go 是 compile 語言，所以編譯後會得到一個很小的執行檔。</li>\n<li>快速（比c慢一點，但比大多數的web語言快</li>\n<li>非常簡單易懂。</li>\n<li>它非常適合應用在微服務的使用場景</li>\n</ul>\n<h2>net/http</h2>\n<p>net/http 為go 的 standard library。它是撰寫restful api 非常好的起始點。</p>\n<p>幾乎所有其它的library都可以與它交互操作。所以理解它是很重要的。</p>\n<p>現在不必了解它所有的細節，只要知道一些我們該知道的事，就可以開始coding了！</p>\n<h2>The Handler Interface</h2>\n<p>下面是 Handler 的介面。所有的 net/http 程式都應該要實現此介面。</p>\n<div class=\"gatsby-highlight\" data-language=\"go\"><pre class=\"language-go\"><code class=\"language-go\"><span class=\"token keyword\">type</span> Handler <span class=\"token keyword\">interface</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token function\">ServeHTTP</span><span class=\"token punctuation\">(</span>ResponseWriter<span class=\"token punctuation\">,</span> <span class=\"token operator\">*</span>Request<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>這個介面只有唯一的一個方法。</p>\n<p>一個結構，只要有一個 <code class=\"language-text\">ServeHTTP</code> 方法，接受 ResponseWriter及 *request，就可以被當作一個handler介面使用。</p>\n<h1>source code</h1>\n<div class=\"gatsby-highlight\" data-language=\"text\"><pre class=\"language-text\"><code class=\"language-text\">package main\n\nimport (\n    \"log\"\n    \"net/http\"\n)\n\ntype server struct {}\n\nfunc (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) {\n    w.Header().Set(\"Content-Type\", \"application/json\")\n    w.WriteHeader(http.StatusOK)\n    // 這個用法應該是指定型態↓\n    w.Write([]byte(`{\"message\": \"hello, world\"}`))\n}\n\nfunc main() {\n    s := &amp;server{}\n    http.Handle(\"/\", s)\n    log.Fatal(http.ListenAndServe(\":8888\", nil))\n}        </code></pre></div>\n<h2>逐行解析</h2>\n<p>所有的可執行go程式，都必須包含一個<code class=\"language-text\">main package</code>。</p>\n<p>當錯誤發生時，我們可以使用<code class=\"language-text\">log</code>來查看發生了什麼事情。</p>\n<p>因為我們要寫 restful api，所以我們必須載入 <code class=\"language-text\">net/http</code>這個套件。</p>\n<p>接下來，我們必須要開始設計我們的伺服器。現在伺服器中沒有任何東西，我們建立一個方法<code class=\"language-text\">ServeHTTP</code>，它會滿足我們的 Handler 介面。</p>\n<p>在go當中，我們不須要明確的指明我們實現了哪個介面。編譯器可以自動偵測我們實現的介面。</p>\n<p>在ServeHTTP中，我們設定 httpStatus 200，表示請求成功。並且設定我們回傳的資料型>態為 <code class=\"language-text\">application/json</code>，好讓 client端知道我們的回傳型態是json檔。</p>\n<p>最後，我們將 <code class=\"language-text\">{\"message\": \"hello world\"}</code>做為回傳值。</p>\n<h1>延伸閱讀</h1>\n<p><a href=\"https://www.ithome.com.tw/voice/107839\">https://www.ithome.com.tw/voice/107839</a></p>","frontmatter":{"title":"初探 Golang","date":"May 26, 2020","description":"嘗試寫第一支 golang","tags":["go"],"template":"post"}}},"pageContext":{"slug":"/2020-05-26-go-starter/","previous":null,"next":{"node":{"fields":{"slug":"/2020-05-28/"},"frontmatter":{"date":"28 May, 2020","title":"RXJS運算子-mergeMap、switchMap","tags":["rxjs","archive"]}}}}},"staticQueryHashes":["2841359383","3810545343","916993862"]}