Preface When we trying to use MongoDB, the requirement comes more and more complex and diversity. Here is some note during my implement. Multiple condition in MongoDB Query It is very easy to find data in MongoDB, but how about multiple condition such as “AND” and “OR” ? AND OR in MongoDB It is very easy to find “AND” support in MongoDB, but how to apply in mgo (MongoDB driver in Go)? // Find user name is John and Contry is US. var alldb []User UserCollection.Find(bson.M{"$and": []bson.M{bson.M{"name": "John"}, bson.M{"Contry": "US"}}}).All(&alldb) Please note: the $and need combine an array of bson.M. // Find CONDITION_A and CONDITION_B bson.M{"$and": []bson.M{ CONDITION_A, CONDITION_B }} So, it is similar with “OR” ($or), detail doc is here. // Find CONDITION_A or CONDITION_B bson.M{"$or": []bson.M{ CONDITION_A, CONDITION_B }} Make it more clear in code. // Find user name is John or Tom. var alldb []User UserCollection.Find(bson.M{"$or": []bson.M{bson.M{"name":...
##前言
前一篇的一些討論後,接下來有一些更容易出錯的部分可以討論.主要focus Goroutine跟 defer
###Goroutine Closure
主要是這一篇部落格帶出的問題:
func main() {
done := make(chan bool)
values := []string{"a", "b", "c"}
for _, v := range values {
go func() {
fmt.Println(v)
done <- true
}()
}
// wait for all goroutines to complete before exiting
for _ = range values {
<-done
}
}
根據以上的部分,印出的結果不會是 “a”, “b”, “c”.而是 “c”, “c”, “c” 原因是 goroutine 變數會參照到go func 跑的時候.
如果修改成以下就可以避免這個問題:
func main() {
done := make(chan bool)
values := []string{"a", "b", "c"}
for _, v := range values {
go func(obj string) {
fmt.Println(obj)
done <- true
}(v)
}
// wait for all goroutines to complete before exiting
for _ = range values {
<-done
}
}
由於他的順序會是 go func(v) 之後才執行,所以其變數內容會先傳過去而不是跑道fmt.Println(v)才取得. 更多跟goroutinem與closure有關的資訊請看這裡Go: FAQ
參考資料
Effective Go: Channels
官方Effective Go文件,一定要熟讀.
Go FAQ: hat happens with closures running as goroutines?
A Go Gotcha: When Closures and Goroutines Collide
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...