2016-04-04 27 views
3

我鑄造到具有在了time.time類型結構圍棋 - 解析NULL來了time.time在結構

t2 := time.Now() 
format := "2006-01-02 15:04:05" 

theTime, _ := time.Parse(format, t2.Format(format)) 

不過,有時我不想設定了time.time領域。 ,你怎麼用go/mysql db驅動來定義這個?

app_history := &models.AppsHistoryInsert{ 
    AppId:   response.SetAppData[0].Id, 
    LiveDate:  &theTime, 
} 

基本上,我想

if(x == true) { 
    include time 
} 
else { 
    don't include time. 
} 

我試圖做一個if周圍結構聲明本身和離開LiveDate場了,但我得到的controllers/apps.go:1068: undefined: app_history

回答

3

您需要定義錯誤app_history變量在if語句之外,然後在每個分支中分配給它

like所以

var app_history &models.AppsHistoryInsert{} 

if x { 
    app_history = &models.AppsHistoryInsert{ 
    AppId:   response.SetAppData[0].Id, 
    LiveDate:  &theTime, 
    } 
}else { 
    app_history = &models.AppsHistoryInsert{ 
    AppId:   response.SetAppData[0].Id, 
    } 
} 
+1

另外,萬一在現實中還有一堆更多的init。按照現在的做法,但排除「LiveDate」,然後只要在創建初始結構後x {app_history.LiveDate =&theTime} –