In this program, we will create a string and get the current date and time using time.Now() function. After that, we will print the result on the console screen.
The source code to get the current date and time is given below. The given program is compiled and executed successfully.
// Golang program to get the current date and time
package main
import "fmt"
import "time"
func main() {
var currentDateAndTime string
currentDateAndTime = time.Now().String()
fmt.Println("Current date and time\n", currentDateAndTime)
}
Output:
Current date and time
2021-03-21 05:50:43.783414013 +0000 UTC m=+0.000076722
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 package that includes the files of package fmt then we can use a function related to the fmt package.
In the main() function, we created a string currentDateTime. Then we called the time.Now() function to get the current date and time. The time.Now() function return the object of the time type, here we used String() function to convert the time into the string. After that, we printed the result on the console screen.
Program/Source Code:
The source code to get the current date and time is given below. The given program is compiled and executed successfully.
Output:
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 package that includes the files of package fmt then we can use a function related to the fmt package.
In the main() function, we created a string currentDateTime. Then we called the time.Now() function to get the current date and time. The time.Now() function return the object of the time type, here we used String() function to convert the time into the string. After that, we printed the result on the console screen.
need an explanation for this answer? contact us directly to get an explanation for this answer