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 介接。 ...

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 取模 標記 FUNDAMENTALS OPTIMIZATION 解題邏輯 若沒有取模限制的話這題可以化簡為 O(1),但很明顯不行。於是先將原題目化簡為 O(n): func FindX(n int) int { mod := int(math.Pow10(9)) + 7 m := 0 for i := 0; i <= n; i++ { m = (m*2 + 3*i) % mod } return m } 好測試過,就算是輸入 1e6 進去也很快就算出來了,於是提交答案 ...