What is Time in Golang ?

 The time package in Go provides functionality for working with dates, times, and durations. Here are some examples of how to use the time package in Go:

1.Getting the current time:

package main

import (

    "fmt"

    "time"

)

func main() {

    now := time.Now()

    fmt.Println("Current time:", now)

}


Output: Current time: 2022-02-24 10:00:00 +0000 UTC

2.Formatting time:
package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Println("Current time in RFC3339 format:", now.Format(time.RFC3339))
}

Output: Current time in RFC3339 format: 2022-02-24T10:00:00Z

3.Parsing a time string:
package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02T15:04:05.000Z"
    str := "2022-02-24T10:00:00.000Z"
    t, err := time.Parse(layout, str)
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println("Parsed time:", t)
}

Output: Parsed time: 2022-02-24 10:00:00 +0000 UTC
These are just a few examples of what you can do with the time package in Go. The package provides many more functions and features for working with dates, times, and durations.

Comments