2014-04-14 8 views

risposta

60

Invocare una struttura vuota time.Time restituirà la data zero di Go. Così, per la seguente dichiarazione stampa:

fmt.Println(time.Time{}) 

L'output è:

0001-01-01 00:00:00 +0000 UTC 

Per ragioni di completezza, il official documentation afferma esplicitamente:

The zero value of type Time is January 1, year 1, 00:00:00.000000000 UTC.

+5

"Passing no arguments" fa sembrare una chiamata di funzione. È una struttura letterale senza campi specificati. X {} è il valore zero della struct X per qualsiasi X. –

+0

@RussCox Non penso che sia vero. Nel mio caso, ho un campo di tempo. Tempo nella mia struttura che ha attributo 'omitempty'. Se non imposto questo valore, viene automaticamente impostato su 0001-01-01 00:00:00 +0000 UTC anziché essere ignorato. –

+0

@GauravOjha Vedi [Golang JSON omitempty With time.Time Field] (http://stackoverflow.com/questions/32643815/golang-json-omitempty-with-time-time-field/32646035#32646035). – icza

125

Y ou dovrebbe utilizzare la funzione Time.IsZero() invece:

func (Time) IsZero 

func (t Time) IsZero() bool 
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC. 
+16

Questa dovrebbe essere la risposta accettata. –

+0

Infatti, se si confronta se il valore dato per il tempo è nullo o no, questo è quello che dovrebbe essere effettivamente utilizzato. –

Problemi correlati