Home »
Golang »
Golang Programs
Golang program to demonstrate the Nanoseconds() function
Here, we are going to demonstrate the Nanoseconds() function in Golang (Go Language).
Submitted by Nidhi, on March 23, 2021 [Last updated : March 04, 2023]
Nanoseconds() function
Problem Solution:
In this program, we will demonstrate the Nanoseconds() function to get the total nanoseconds by specifying the time in a specific format.
Program/Source Code:
The source code to demonstrate the Nanoseconds() function is given below. The given program is compiled and executed successfully.
Golang code to demonstrate the example of Nanoseconds() function
// Golang program to demonstrate the
// Nanoseconds() function
package main
import "fmt"
import "time"
func main() {
t, _ := time.ParseDuration("3h32m30s")
fmt.Printf("Total Nanoseconds are: %d", t.Nanoseconds())
}
Output:
Total Nanoseconds are: 12750000000000
Explanation:
In the above program, we declare the package main. The main package is used to tell the Go language compiler that the package must be compiled and produced the executable file. Here, we imported the fmt and time packages to use time and fmt related functions.
In the main() function, we created t variable initialized using time.ParseDuration() function. Then get total nanoseconds using Nanoseconds() function. The Nanoseconds() function returns int64 value. After that, we printed result on the consoles screen.
Golang Date & Time Programs »