función time.Now() en Golang con ejemplos

En el lenguaje Go, los paquetes de tiempo brindan funcionalidad para determinar y ver el tiempo. La función Now() en el lenguaje Go se usa para encontrar la hora local actual. Además, esta función se define en el paquete de tiempo. Aquí, debe importar el paquete «tiempo» para usar estas funciones.

Sintaxis:

func Now() Time

Valor devuelto: Devuelve la hora local actual.

Ejemplo 1:

// Golang program to illustrate the usage of
// time.Now() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Calling Now() method
    tm := time.Now()
  
    // Prints current 
    // local time in UTC
    fmt.Printf("%s", tm)
}

Producción:

2020-04-09 11:24:14.785868848 +0000 UTC m=+0.000187421

Aquí, la hora actual se devuelve en UTC.

Ejemplo 2:

// Golang program to illustrate the usage of
// time.Now() function
  
// Including main package
package main
  
// Importing fmt and time
import "fmt"
import "time"
  
// Calling main
func main() {
  
    // Calling Now() method
    tm := time.Now()
    tm1 := time.Now()
  
    // Prints current local time in UTC
    fmt.Printf("%s\n", tm)
  
    // Sleep for 10 seconds
    time.Sleep(10 * time.Second)
  
    // Prints the current time after 10
    // seconds of the sleep is over
    fmt.Printf("%s", tm1)
}

Producción:

2020-04-09 11:37:59.087484568 +0000 UTC m=+0.000106559
2020-04-09 11:37:59.087484779 +0000 UTC m=+0.000106736

Publicación traducida automáticamente

Artículo escrito por nidhi1352singh y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *