2019年10月3日 星期四

golang context 讓程式變髒了


為了解決goroutine間(特別是多層樹狀)取消( cancelation )
在 go 1.7 提供了 context standard library。
https://blog.golang.org/context

雖然提供了方便簡潔的操控goroutine的取消方法,並利用channel自動的通知取消給所有子執行緒。以及讓整顆樹的goroutine都能共享資料。

但官方希望我們不要二次封裝 context,而採用參數方式傳到每一個相關的function裡,連變數名稱都幫我們想好( ctx ),而且還建議放第一個參數。
這.... ,說真的,我不太能接受。

我們原本優雅的程式,讓這 ctx 污了整個code。

func doSomething(...interface{}) {
...
}
硬生生要變成這樣...
func doSomething(ctx context.Context, ...interface{}) {
...
   ctx.Done()
}

context 讓程式變髒了,就為了執行 ctx.Done(),ctx.Value(key)

@@

其實在讀完context的原始碼後,大概能理解原由。
雖然context source code寫得相當精要傳神,但個人認為其還是有許多的改善空間。


2019年9月24日 星期二

golang sqlbuilder

https://github.com/eehsiao/sqlbuilder

sqlbuilder

sqlbuilder is a simple sql query string builder
sqlbuilder its recursive struct call, that you can easy to build sql string
ex: dao.Select().From().Join().Where().Limit()

SqlBuilder functions

  • build select :
    • Select(f ...string)
    • Distinct(b bool)
      • its default in builder is set false
    • Top(i int)
      • only support mssql
    • From(t ...string)
      • t is table name
    • Where(c string)
      • c is condition, ex Where("field1=1 and filed2='b'")
    • WhereAnd(c ...string)
    • WhereOr(c ...string)
    • Limit(i ...int)
      • support 2 parms
      • only support mysql
    • Join(t string, c string)
      • t is table name
      • c is condition
    • InnerJoin(t string, c string)
    • LeftJoin(t string, c string)
    • RightJoin(t string, c string)
    • FullJoin(t string, c string)
    • GroupBy(f ...string)
      • f is a fileds list of strings
    • OrderBy(f ...string)
      • f is a fileds list of strings
    • OrderByAsc(f ...string)
    • OrderByDesc(f ...string)
    • Having(s string)
      • s is having condition string
    • BuildSelectSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • build update :
    • Set(s map[string]interface{})
    • FromOne(t string)
      • reset the table for only one
    • BuildUpdateSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • build insert :
    • Into(t string)
      • set the insert table
    • Fields(f ...string)
      • f is a fileds list of strings
    • Values(v ...[]interface{})
      • v is a values list of interface{}
    • BuildInsertSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • build delete :
    • BuildDeleteSQL()
      • check and build sql string.
      • you can get sql string via BuildedSQL()
  • common :
    • ClearBuilder()
      • reset builder
    • BuildedSQL()
      • return the builded sql string, if build success.
    • SetDbName(s string)
    • SetTbName(s string)
    • SwitchPanicToErrorLog(b bool)
    • PanicOrErrorLog(s string)

2019年9月17日 星期二

Inter-Process Communication via websocket (golang)

github : https://github.com/eehsiao/websocket-ipc

websocket-ipc

Inter-Process Communication via websocket

how to be a deamon

install : install into service start : run as a deamon
sudo ./example install
sudo ./example start

how to stop deamon

when the deamon start
sudo ./example stop

how to remove deamon

when the deamon stop
sudo ./example remove

how to Communication

when the deamon start
sudo ./example {any string otherwise commands}