I have the following program:
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now().UnixNano() / int64(time.Millisecond)
nowString := string(now)
fmt.Println(nowString)
}
I'm expecting the epoch time to be printed as a string. Instead I get:
�
How do I fix this error?
You are doing a lot of calculations which are not needed if you use time
's functions as exemplified here - https://gobyexample.com/time-formatting-parsing
Also. if all you need to do is to print an integer, you don't have to convert it to a string but simply use a format specifier like:
func main() {
now := time.Now().UnixNano() / int64(time.Millisecond)
//now is an int64 as you may have observed
fmt.Printf("%d", now)
//or even fmt.Println(now)
}
main.go
package main
import "fmt"
import "time"
func main() {
nanos := time.Now().UnixNano()
fmt.Println(time.Unix(0, nanos))
millis := nanos / 1000000
fmt.Println(millis)
}