Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert nano time to string return garbage? #39089

Closed
proyb6 opened this issue May 15, 2020 · 1 comment
Closed

Convert nano time to string return garbage? #39089

proyb6 opened this issue May 15, 2020 · 1 comment

Comments

@proyb6
Copy link

proyb6 commented May 15, 2020

What version of Go are you using (go version)?

1.14.3

Does this issue reproduce with the latest release?

I think earlier may have the same issue, I have checked other repos have used the same approach.

What operating system and processor architecture are you using (go env)?

NA

What did you do?

Converting nano time to string

fmt.Println(string(time.Now().UnixNano()))

What did you expect to see?

As expected number return by nano time.

What did you see instead?

A question mark character

https://play.golang.com/p/7NfnDfYy4_N

@acln0
Copy link
Contributor

acln0 commented May 15, 2020

string(x) doesn't do what you think it does when x is an integer. It interprets x as a unicode code point and creates a string with the UTF-8 encoding of said code point, if possible. For your particular case, it isn't possible, so you get an encoding of unicode.ReplacementChar instead: https://play.golang.org/p/qLfoV6dj_AK

To format an integer into a string, use the fmt or strconv package.

@proyb6 proyb6 closed this as completed May 15, 2020
@golang golang locked and limited conversation to collaborators May 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants