Golang Struct 與 JSON 的快速轉換法
作為前 PHP 開發者,一說到 Golang 要把東西轉做 JSON 就頭痛,因為在 php 裡面你可以非常簡單的把一個複雜的結構轉換成 json,例如說: 就能夠輸出 所以在開發 php 的時候基本上是不用特別處理 json 的轉換的,到了 javascript 一端也能夠輕易的把收回來的資料直接使用,例如: $.get("url_here",function(data){ console.log(data[0]); }); // 輸出 "Hello" 但是去到 Golang 之後寫法就完全不同了,在這篇簡短的文章中,我會簡單介紹一下怎樣輕易的把任何資料 parse 成 JSON string 及由 JSON string 轉回原本型態的方法 Golang 的 Struct 是一樣很神奇的東西 甚麼是 Golang Struct 呢?這個跟 C 語言裡的沒差太遠,簡單來說就是 Golang 上 OOP 的方法。以下是一個簡單的 Struct 例子(原碼取自 ArOZ Online 1.0) type desktopObject struct{ Filepath string; Filename string; Ext string; IsDir bool; IsShortcut bool; IconX int; IconY int; } 把 Struct 轉換成 JSON 這是一個用來定義桌面上的物品的 struct。要把它轉到 JSON 十分簡單,首先你要新建一個 Object thisFileObject := new(desktopObject) 然後把資料填進去,例如這樣 thisFileObject.Filepath = path; thisFileObject.Filename = filepath.Base(path) thisFileObject.Ext = filepath.Ext(path) thisFileObject.IsDir = IsDir(path) 最後把它壓成一個 JSON String jsonString, err:= json.Marshal(desktopFiles); if (err != nil){ //錯誤處理 } return string(jsonString); 就是這樣你的 JSON String 就做好了 由 JSON 轉回 Struct 這個就難一點了,但是還是能做到的。簡單來說就是把 JSON string map 到一個現有的 struct 上面去。先假設我們有以下的一個 struct type iconLocation struct{ X int; Y int; } 和一個 JSON string jsonstring := "你要轉換的東西" 首先你跟上面做的一樣,先預留一個變數給 json unmarshal 時用。然後把 json string 轉換到這個 struct 裡面。 thisLocation := new(iconLocation) json.Unmarshal([]byte(jsonstring ), &thisLocation) fmt.Println(thisLocation) 這樣你就完全把 JSON string 轉回去 struct 了。有夠簡單吧?
WAMP SERVER MySQL 無法連線,因為目標電腦拒絕連線。
最近在幫伺服器由 php 7.0 升級到 php 7.4.4,然而在升級 wordpress 的時候出現了無法連接到資料庫的問題,於是簡單的從網上找到了一個測試資料庫的 php script,發現原來不是 wordpress 的問題,是 MySQL 的問題。 $DBServer = 'localhost'; $DBUser = 'username'; $DBPass = 'password'; $DBName = 'wordpress'; $link= new mysqli($DBServer, $DBUser, $DBPass); if(!$link) echo "失敗!"; else echo "成功!"; 錯誤 解決方法:在 localhost 後面加上 :3307 結果在網上找了一整天也找不到能處理的解決方法,於是在想會不會是 port 設定的問題呢?於是在 localhost 後加上了 :3307 $DBServer = 'localhost:3307'; 然後居然就 fix 好了!? 不過話說,在 Fix 這個 bug 的時候整個網頁伺服器上的服務都不能用,除了其中一個完全不依賴 Database 的服務,我想我不用說你也已經猜到了: ArOZ Online 系統!這套系統在 PHP Extension 一個都沒開 + MySQL 完全 offline 的情況下也能繼續正常運作還真的是滿壯觀的。 升級途中居然對 ArOZ 系統一點影響都沒有,還能夠一邊聽音樂一邊升級 就是這樣,伺服器就有驚無險地升級上 PHP7.4.4 囉! P.S. 有人問邊到睇到 MySQL 個 port你可以係 WAMP 個 menu 下面找 MySQL → Port used by MySQL: 3307
使用 PHP 啟動 Golang Binary 的 DIRECTORY SEPARATOR 問題
在開發 AOB 的時候不知道為啥 Windows 開發的 Websocket JWT 登入系統在 Linux 上跑不動,然後 Golang 寫的 WS Server 還不斷噴 Error,一開始還以為是 Golang 的問題,最後原來是啟動 Golang binary 的 PHP 的問題 。 Golang WebSocket Server 在 parse PHP 輸出的 JSON 時出現了 "<" 無法解釋所以 panic 了 在 Windows 上取得由 Web Root 到 Authentication Service Script 位置的代碼,Linux 上會回傳 "" (Empty String),必須移除有關 / 跟 \ 的處理部分才能使用。 //Parse the launch paramters if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { //Windows. Parse the launching path for validation services from wamp service webroot $fullPath = str_replace($_SERVER['DOCUMENT_ROOT'],"",realpath(str_replace("\\","/",__DIR__) . "../../jwt/validate.php")); $fullPath = str_replace("\\","/",$fullPath); $launchpath = str_replace($_SERVER['DOCUMENT_ROOT'] . "/","",$fullPath); }else{ //Life is always simplier on Linux $launchpath = str_replace($_SERVER['DOCUMENT_ROOT'] . "/","",realpath("../jwt/validate.php")); } 這問題主要出在 PHP 可以把 / 跟 \ 同時當作 "/" ,所以在程式裡作為 Directory Separator 用 "/" 就好,不需要特別處理,但是在 Golang 裡 "/" 跟 "\" 是不同的符號,必須要先做好預處理再 feed in 到裡面。 (你問我為啥不用 DIRECTORY_SEPARATOR ?因為測試過這東西也不能用啊?特別是要經過 realpath() 的時候特別容易出錯)
開發使用 Webcam 的網頁應用時找不到navigator.mediaDevices
如果你在開發使用 Webcam 的網頁的時候遇上這個錯誤: TypeError: navigator.mediaDevices is undefined Open Vtuber Studio 裡面顯示的錯誤 那是因為你在用 Firefox 而的網頁並非使用https 加密。然而你又太懶不打算幫你的實驗用網頁伺服器裝上 SSL 憑證,那你可以跟著以下步驟解決: 轉用 Google Chrome在網址列輸入 chrome://flags/#unsafely-treat-insecure-origin-as-secure 把你的網站加入到例外清單把此功能開啟(Enable) 在我工作室的測試伺服器設定例子 設定之後重啟一下 Chrome 即可解決。至於 Firefox 上這問題依然是無解,除非你自己重新組建一個 Firefox 出來。
PHP7 下檔案名稱太長導致 glob 回傳 false 的問題
很多時候在開發 PHP 的時候我們都會使用這個超級方便的 glob(*) 功能。簡單來說就是把一個資料夾或路徑內的檔案以 array 方式回傳的方法。然而,在最近的 ArOZ Online 測試伺服器更新之後發現了 File Explorer 在開啟某些資料夾時出現 PHP ERROR 的問題。後來經過一堆檢測之後發現原來是檔案名稱太長了,結果 glob 就直接回傳 false 。 原本的 listdir.php //Some code here for auth and var parsing if (file_exists($path) && is_dir($path)){ $filelist = glob($path . "/*"); //Other code } 要解決這個問題,我們需要把它改成使用 scandir ,然而由於 scandir 會多了很多代碼,為了節省處理能力,我們只在需要的時候使用 scandir 就好。結果新的 glob 部分就變成了這樣: //Some code here for auth and var parsing if (file_exists($path) && is_dir($path)){ $filelist = glob($path . "/*"); if ($filelist === false){ //Something went wrong while trying to list the directory using glob. Try scandir $scanResults = scandir($path); $filelist = []; $useAbnormalFilter = true; foreach ($scanResults as $fileObj){ if ($fileObj != "." && $fileObj != ".."){ array_push($filelist, $path . "/" . $fileObj); } } } //Other code } 就是這樣,File Explorer 的顯示就回復正常了。對,這都是 Windows 的錯。
How it works
IMUS Laboratory Product Development and Prototype Resale Welcome to IMUS Laboratory PD-PR web page. This post will explains how this page works. Basic work flow Tell us what you want to make, the more detail you give, the better the results and pricing accuracy. Tell us your target pricing or the amount you are willing to give for this prototype. MAKE SURE YOUR REQUEST IS "DESIGN SKETCH ONLY" or "PROTOTYPE READY" We will provide a rough pricing for your prototype (The final pricing may be greater or lower than the expected price) If you agree with the price, the project will be started. If you disagree with the price, you can try removing some functions or cancel the prototype request.        
Hello world!
This page was started on 12/9/2016.
目前第 4 頁,共有 4 頁