[Golang][程式設計週記].. 2015第九週

##雜七雜八感言: 最近搞完一些Golang部分,有開始跑各個平台的cross platform的building solution.果然C++跨平台一點都不簡單,真是80%設定環境,20%寫跨平台的code…. ##筆記: [Golang] 關於Golang 有趣的網頁與小工具 GophoerConIndia 許多Go 的大老都參加了,挺值得一看. Go on Mobile - David Crawshaw - London Go Gathering 2015 [Apple] 關於 Apple 03/09 發表會 這個禮拜讓蘋果迷最興奮的就是三月的發表會. 裡面有包括了ResearchKit,MacBook更新跟Apple Watch. 兩個跟硬體有關的就沒啥好評論…. ResearchKit 與 HealthKit 根據已經放出的消息,ResearchKit透過兩項(目前已知是手指點擊與聲音)的手機功能,可以提供一些簡單的診斷.並且把這些資料提供給醫療研究機關. 等到詳細資料出來應該會更仔細地研究一下,是否一般廠商有介入的空間. [Android] 在Android Stuido 1.1 上面使用 自從Google 推出 Android Studio 正式版之後,自然而然也開始把Eclipse的力道放輕. 於是要弄新的SDK也變成是一個工程. 所以現在要開發新的cross platform module,所以也必須要在 Android Studio + NDK + JNI 去執行跨平台的 C++ module. 根據最新版的Android Studio 1.1 如果依照著一般的方式來部署NDK會發現以下的問題: WARNING [Project: :app] Current NDK support is deprecated. Alternative will be provided in the future. 找了一堆論壇,其實並沒有一個有系統地整理,直到看到這一篇文章,以下把方法整理一下: 安裝 Android Studio 1.1 下載並且安裝 Android NDK r10d 先到一開始的畫面[Configure]->[settings]->[External Tools] 設定Gradle 設定javah 新增一個NDK Build 類別為NDK Program:C:\ndk\ndk-build.cmd Parameters:NDK_PROJECT_PATH=$ModuleFileDir$/build/intermediates/ndk NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs NDK_APPLICATION_MK=$ModuleFileDir$/src/main/jni/Application.mk APP_BUILD_SCRIPT=$ModuleFileDir$/src/main/jni/Android.mk V=1 Working directory:$SourcepathEntry$ 這樣設定好就可以 複製ndk-r10d 目錄下的sample\hello-jni來使用 會出現一個[c]在上面按下 右鍵跑 “NDK Build”,來製造出.so [App]->[build.gradle]新增: sourceSets.main.jni.srcDirs = [] 然後sync gradle 這時候會發現[C]不見了,就可以跑App 之後要切換就是把上面的那段 comment掉才能build NDK
繼續閱讀

[Android/Golang]Server-Side In-App-Purchase Verification with Google Play on GO

Preface Working on Android application development, you will need to have IAP (In-App Purchase) items. Normally it is simple, if your application is standalone not connect to any server. If you app need connect to server for IAP items (such as game server, database service …), it might have risk that here might be a fake app (or crack app) to fake the purchase command in your app to get privilege action or items. In this case, our server will need to do a sever-to-server side certification with Google Play. Android has done great documentation in their Android portal. But it separate into different part, so I am trying to summarized it here. Hope it help. Google Service Entrypoint - Google API When we want to communication with any Google Service, the only entry point is using Google API Console.. So, here let’s start to connect to Google API. According...
繼續閱讀

[Golang] 關於Golang Time 的處理

[Golang] 關於Golang Time 的處理 花了一點時間在處理timestamp.工作不外乎是儲存timestamp,解析timestamp並且比對timestamp. 找了很久的相關資料,卻發現處理上並沒有那麼直覺主要是在於以下幾個部分. ####格式: 永遠會困在格式之中 首先最重要的就是處理time的格式,根據Golang Time Pkg,裡面的格式可以有ANSI, RFC822, RFC1123…. 許多種,這裡面大家所熟知的ISO 8601其實沒有名列在其中.但是你可以使用它其中一個profile 那麼就是RFC 3339 儲存格式決定你要如何存取時間的資料. 當然…. 如果大家都是丟time.Time 的格式.. 那麼就世界太平…. 如果不行.. 大家談一下如何parse.. ####時區: 時間比對的重要參數 大家都知道,時區是一個濫觴.但是只要能夠正確的處理其實Golang裡面內建已經把時區處理好了.主要用來解析的是Parse與ParseInLocation 會需要使用到時區的時候,通常是在於接收資料從client或是其他的程式. 這時候比較建議的是: 統一使用UTC 時區,作為字串解析會統一點 LoadLocation 是個好東西,可以幫助你轉換時區.前提是你已經拿到time.Time 以下為一個簡單範例去取得UTC的時間 func GetUTCTime() time.Time { t := time.Now() local_location, err := time.LoadLocation("UTC") if err != nil { fmt.Println(err) } time_UTC := t.In(local_location) return time_UTC } 如果你必須要parse一個time.Time不支援的格式,來轉換成time.Time格式.以下是一個簡單的範例.(當然前提資料是 UTC :) ) func ParseCustomTimeString(time_string string) time.Time { //"2015-03-10 23:58:23 UTC" str_array := strings.Split(time_string, "UTC") var year, mon, day, hh, mm, ss int fmt.Sscanf(str_array[0], "%d-%d-%d %d:%d:%d ", &year, &mon, &day, &hh, &mm, &ss) time_string_to_parse := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d+00:00", year, mon, day, hh, mm, ss) url_create_time, _ := time.Parse(time.RFC3339, time_string_to_parse) return url_create_time } ####時間的比對,善用Before/After 針對於時間的操作上面,其實Golang Time Pkg 提供了很方便的比較時間先後的方式. Before/After 其實只要透過以下的方式,就可以比較不同時區,不同時間格式的先後順序. func IsWarrantyExpired(time_purchase time.Time) bool { //time of product purchase store in DB which is time.time with ETC time_Warranty := time_purchase.AddDate(1, 0, 0) //假設是一年保固 return time_Warranty.After(time.Now()) //如果保固期比現在還後面,代表還是保固期內. } 範例程式中, time.Time.AddDate() 是一個可以取出變更過的時間.比如說你需要取出現在這個時間一年後的timestamp,就可以透過這個.目前支援年,月,日. 如果要小時的話,可能就得要手動去改. ####在資料庫裡處理time.Time 其實不論是 MySQL或是...
繼續閱讀

[XMPP][Ejabberd] Some XMPP spec survey

Preface: This article is a note about I study XMPP spec recent days. To maniplate a XMPP client, it might be easy to deal it with IP*WORKs 3rd party XMPP client. But it could not fulfil some custom request such as: Not response friend request immediatelly, once we got it. Handle roster (friend list) programmatically Maniplate VCard as custom information storage. In those case, we might need to handle XMPP XML commands to Ejabberd Ejabberd is a XMPP server(which twitter use it at first). It written by Erlang, it is powerful to handle multiple user connectivity. XMPP Command Both we could have two ways to manipulate XMPP, one by 3rd party XMPP framework. We will use IP*Work for a example which you can find sample code from web here. Another one is using basic XML send to XMPP server directly. Actually we still use IP*Works. sendCommand to send our XML...
繼續閱讀

[Golang][程式設計週記].. 2015第九週

##雜七雜八感言: 開始把一些內容分成別篇文章,最近多花了很多時間讀XMPP Spec挺有趣的. ##筆記: [Golang] 關於Golang 有趣的網頁與小工具 GRPC: Go Project 支援HTTP/2之外,還支援九種以上的語言.並且可以擺脫net/rpc原本需要管理gob的痛苦. Gore: Yet another Go REPL that works nicely. Featured with line editing, code completion, and more. 另外一個Go REPL的工具,支援許多shortcut跟即時顯示… Viper: A configuration tool of Go 用來讀取與使用Config的工具,開發者是spf13 Hugo的開發者.當然,Hugo也有使用Viper Wide: A Web-based IDE for Teams using Golang. https://wide.b3log.org 相當酷的網站,可以online 寫go而且有資料夾的概念.並且有syntax highlighter的. [Golang] Meetup Youtube Playlist: London Go Gathering February 2015 FOSDEM 2015 Video list Go at CoreOS by +Kelsey Hightower​​ This session will discuss using Go to build products that make distributed computing as stress-free as installing a Linux distribution. Finding Bad Needles in Worldwide Haystacks by Dmitry Savintsev Experience of using Go for a large-scale web security scanner Moving MongoDB components to Go by +Norberto Leite​​ We love Go and this train is unstoppable! CockroachDB by +Tobias Schottdorf​​ Towards an Open-Source Spanner HTTP/2 for Go by +Brad Fitzpatrick​​ Overview of HTTP/2 and the design of Go’s support for it Go and...
繼續閱讀

[Golang]關於Go的Constant的型別轉換(Type Cast)

前言: 原本的問題是出現在Stackoverflow,有人發問template.HTML(“test”) == “test” 可以過,但是template.HTML(“test”) == some_str 卻不能成 功compiler. 原因: 主要的原因是由於Go 本身是static typing的程式語言,而constant本身的型別是未定,並且對於constant在compiler的狀態下是會有一些型別轉換的. // 型別尚未確定 const hello_string = "Hello, 世界" // 型別確定為字串 fmt.Printf(" type is %T \n", hello_string) //type is string 範例程式碼: 根據The Go Blog: Constants 裡面有詳細提到關於Go 是 statical typing的一些問題. 接下來用幾個範例解釋: package main import ( "fmt" ) func main() { // hello Type is not define for now. const hello = "Hello, 世界" // Type is String const typedHello string = "Hello, 世界" // hello type is cast to string fmt.Printf("Type of hello is=%T, typedHello is=%T \n", hello, typedHello) // hello is non-type it convert to string and compare fmt.Println(hello == typedHello) //Another sample. Define MyString as a string type type MyString string var m MyString // m is non-type for now. m = "Hello, 世界" // It will introduce error, because m type is MyString and typeHello is string. //fmt.Println(m == typedHello) // hello is non-type until compare with...
繼續閱讀