Home > Backend Development > Golang > What are the functions related to time processing in Go language?

What are the functions related to time processing in Go language?

王林
Release: 2023-06-09 19:51:13
Original
1252 people have browsed it

Go language is an emerging programming language. Its efficiency and simplicity have attracted more and more developers. Manipulating time is a common need when writing programs, so the Go language provides many functions related to time processing. This article will introduce some commonly used time processing functions.

  1. time.Now()

time.Now() function returns the current time. The time returned by this function is a value of type time.Time. For example:

package main

import (
    "fmt"
    "time"
)

func main() {
    now := time.Now()
    fmt.Println(now)
}
Copy after login

Output: 2021-08-20 11:12:48.693123 0800 CST m= 0.000102671

  1. time.Parse()

time The .Parse() function parses a string into a value of type time.Time. Formatted strings must follow specific rules to indicate the time that the string to be parsed represents. For example:

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "2006-01-02 15:04:05"
    str := "2021-08-20 10:10:10"
    t, err := time.Parse(layout, str)
    if err != nil {
        fmt.Println(err)
    } else {
        fmt.Println(t)
    }
}
Copy after login

Output: 2021-08-20 10:10:10 0000 UTC

  1. time.Unix()

time.Unix( ) function converts a Unix timestamp to a value of type time.Time. A Unix timestamp is the number of seconds that have elapsed since January 1, 1970 00:00:00 UTC. For example:

package main

import (
    "fmt"
    "time"
)

func main() {
    unixTime := int64(1629488400)
    t := time.Unix(unixTime, 0)
    fmt.Println(t)
}
Copy after login

Output: 2021-08-20 10:20:00 0000 UTC

  1. time.Duration()

time.Duration( ) function represents a time interval in nanoseconds. The time interval is a value of type time.Duration. For example:

package main

import (
    "fmt"
    "time"
)

func main() {
    t1 := time.Now()
    time.Sleep(time.Second)
    t2 := time.Now()
    duration := t2.Sub(t1)
    fmt.Println(duration)
}
Copy after login

Output: 1.000743896s

  1. time.After()

time.After() The function waits for a period of time and returns a channel. The channel will receive a value after the specified time. For example:

package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Println("start")
    <-time.After(time.Second * 3)
    fmt.Println("end")
}
Copy after login

Output:

start
end

The above is an introduction to some functions related to time processing in the Go language. In actual development, we also need to use other time processing functions according to specific needs. When using it, we should understand the role and usage of each function to make full use of time processing functions to improve code efficiency.

The above is the detailed content of What are the functions related to time processing in Go language?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template