Golang 學習筆記 Day 1 - Getting started

免責聲明,此篇為個人學習筆記,非正式教學文,僅供參考感謝指教,有任何內容錯誤歡迎發 issue。 在前一天介紹了為何要使用 golang、安裝過程以及基本的 hello world,這天將重點放在最基本的 golang 程式碼特徵。 packages 正如昨天關於 hello world 的解析中提到的,每個程式都是由一到多個 packages 組成的,也就是 .go 中必須宣告該檔案是哪個 package,且同一個資料夾中僅接受 xxx 以及 xxx_test 兩種 package名稱,也就是該 package 的名稱以及測試檔的「測試包」。 至於 package 要怎麼命名呢,首先要提到檔案該如何命名,雖然 go format 是對於程式碼風格十分嚴格的,但是對於檔案名稱似乎就沒那麼嚴謹,但整個社群中普遍會用 snake_case 或是 dash-case 作為檔案 (甚至是 package) 的命名。 aaabbbccc.go time-machine.go battery_charger.go 但是需要注意的是 go build 會忽略所有以 _ 或是以 . 作為結尾的後綴, underline 可能會導致在建構時被忽略。 再來提到 package 到底要如何命名呢,官方文檔中明確的提到不要用 camelCase 以及 snake_case 。 The style of names typical of another language might not be idiomatic in a Go program....

2020-08-25 · 2 min · 383 words

RESTful server based on golang

很高興有個機會製作面試前的測試作業,也就趁這個機會做個筆記以及學習紀錄。 前置作業 先點列一下項目需求: 基本需求 based on golnag The API interface you provide can be any of the following:RESTful、json rpc、gRPC At least two sources When a source is unavailable, the result of its last successful ask is returned Use git to manage source code Write readme.md and describe what features, features, and TODO which have been implemented 額外可選 Traffic limits, including the number of times your server queries the source, and the number of times the user queries you Good testing, annotations, and git commit An additional websocket interface is provided to automatically send the latest information whenever the market changes Users can choose to use an automatic source determination, the latest data source, or manually specify the source of the data Package it as a Dockerfile, docker-compose file, or kubernetes yaml There is a simple front-end or cli program that displays the results The API you provide has a corresponding file, such as a swagger, or simply a markdown file Other features not listed but that you thought would be cool to implement 這邊就必須要先打預防針了,這篇僅為學習筆記,並非正式教學。且,我只是 Golang 初學者,所以這篇內也不會出現什麼高等級的解法,甚至也是第一次實作介接 API 呢 (笑)。而 DB 以及 redis 都是直接以方便使用去寫 dbi 而已並無用抽象曾去封裝,也進而導致 unit test 十分困難先擱置了,有空才會再針對性重構,而此次就先將目標專注在 API 介接。...

2020-03-19 · 5 min · 974 words

Codewars Find X II in golang

想不到一眨眼半個月過去了,自從面試趨勢之後整個人彷彿被電到失去信心,在離職與等待面試消息期間,也只好振作補足不足的地方繼續向前。 而 docker 學習心得何時會產出呢?在學習過程中發現除了一般操作(day1 內容)外,底層或多 container 溝通都是大量的網路觀念與知識,需要更多知識量才能整理成筆記,不急不急。 由於學習過程發現而這段時間在 coding 能力上主要還是靠 codewars 上的題目,畢竟解完後能看別人的 code 來檢視自己的 code 能不能更好。其中不免遇到一些覺得很有趣的題目,好比這題 Find X II,他並不是什麼過分難的題目,邏輯也不是很難通,畢竟只是一個 6 kyu 的題目,但為何快一年了只有 44 個人解完呢,這就牽扯到下面要說的。 題目概要 有下一段程式碼: func FindX(n int) int { if n == 0 { return 0 } x := 0 for i:=1; i<=n; i++ { x += FindX(i-1) + 3*i } return x } 但 x 範圍為 1 <= n <= 10**6(1e6) ,請試著重構。且當 n 愈來試大時,可能會超過 int64 表達,故需要將結果對 10**9 + 7 取模...

2020-03-03 · 1 min · 188 words