使用go-sql-driver來連接mysql數(shù)據(jù)庫蛀蜜,獲取的時(shí)區(qū)默認(rèn)是UTC +0的,與本地的東八區(qū)是有區(qū)別赃绊,在業(yè)務(wù)處理中會(huì)出現(xiàn)問題俐镐,解決方案:
- 連接dsn時(shí)候添加loc=Local參數(shù)即可,這樣寫入的datetime類型就會(huì)按照東8區(qū)正常顯示
樣例:
url := cfg.User + ":" + cfg.Pwd + "@tcp(" + cfg.Ip + ":" + common.Itoa(cfg.Port) + ")/" + cfg.Database + "?charset=utf8&loc=Local"
db, err := sql.Open("mysql", url)
···