Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created August 21, 2018 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/413c6fd7a181e51b775fc829a19cffb9 to your computer and use it in GitHub Desktop.
Save xeoncross/413c6fd7a181e51b775fc829a19cffb9 to your computer and use it in GitHub Desktop.
Store a list of 64bit integers as a long slice of bytes in Go https://play.golang.org/p/XMQVJUgkZ9T
package main
import (
"crypto/rand"
"encoding/binary"
"fmt"
)
func main() {
// The actual index is a N * 8 []byte long slice of values
var index []byte
var generatedIds []uint64
for i := 0; i < 10; i++ {
// Generate a bunch of random 64bit numbers (in []byte form)
// id := rand.Int63()
ib := make([]byte, 8)
rand.Read(ib)
id := binary.BigEndian.Uint64(ib)
generatedIds = append(generatedIds, id)
index = append(index, ib...)
}
fmt.Println("index", index)
fmt.Println("generatedIds", generatedIds)
var ids []uint64
for i := 0; i < len(index); i += 8 {
id := binary.BigEndian.Uint64(index[i : i+8])
ids = append(ids, id)
}
fmt.Println("ids ", ids)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment