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

Base64 should encode bytes rather than string representation of int64 #62

Open
jiaoyuedave opened this issue Dec 21, 2023 · 1 comment

Comments

@jiaoyuedave
Copy link

func main() {

	// Create a new Node with a Node number of 1
	node, err := snowflake.NewNode(1)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Generate a snowflake ID.
	id := node.Generate()

	// Print out the ID in a few different ways.
	fmt.Println(id)
	fmt.Println(id.Base64())
	b := id.IntBytes()
	fmt.Println(base64.StdEncoding.EncodeToString(b[:]))
}

result:

-129775853767749632
LTEyOTc3NTg1Mzc2Nzc0OTYzMg==
/jLxjG/AEAA=

dig into souce, i found that base64 method use string representation of int64 for encoding, which make no sense:

// Base64 returns a base64 string of the snowflake ID
func (f ID) Base64() string {
	return base64.StdEncoding.EncodeToString(f.Bytes())
}

// Bytes returns a byte slice of the snowflake ID
func (f ID) Bytes() []byte {
	return []byte(f.String())
}

it only make representation much longer.

@bwmarrin
Copy link
Owner

bwmarrin commented Jan 8, 2024

Thanks for the report, and good catch on this. Let me look at it and decide what we can do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants