Created
August 21, 2018 15:54
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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