Use `encoding/binary`. Yes, you have to care about byte order. But that's
inherent to the question. There is no canonical way in which an integer
correspond to a slice of bytes, you actually have to say how to encode it.
That also doesn't cause any problems - on the contrary, being explicit
about that means you *don't* get into issues (for not knowing what byte
order data is in). See also:
https://commandcenter.blogspot.com/2012/04/byte-order-fallacy.html

So, you probably want to do
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, uint64(v))
and
v := int64(binary.BigEndian.Uint64(b))

On Mon, Nov 21, 2022 at 9:57 AM Nikhilesh Susarla <nikhilesh1...@gmail.com>
wrote:

> Hi,
>
> I have an int64 value say 12
> I want to convert that to []byte array.
> How do we do that. There were lot of ways on the internet. Nothing which
> golang specifies or has a built-package for direct conversions. Lot of them
> dealt with Endiean's, but that might cause issues to I believe.
>
> So, is there any one recommended and safe way?
> Thank you
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/dffa4b81-9c7b-48d8-8b8b-262804756606n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/dffa4b81-9c7b-48d8-8b8b-262804756606n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAEkBMfEfhYYOvLdvZri1_jJ%3DVYeGGyYB9UjXJX3Ud%2BKya%2BiUpg%40mail.gmail.com.

Reply via email to