2016-04-04 20 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 so

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} –

関連する問題