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

qrcode.Encode accept and []byte instead of string #55

Open
bokunodev opened this issue Dec 29, 2021 · 3 comments
Open

qrcode.Encode accept and []byte instead of string #55

bokunodev opened this issue Dec 29, 2021 · 3 comments

Comments

@bokunodev
Copy link

bokunodev commented Dec 29, 2021

Hi, we are using this library to encode output from json.Marshal into a qrcode.
but converting []byte to string is a copy operation, and we want to minimize that.
having qrcode.Encode variant that accept []byte instead of string would be great.
probably i'll be named qrcode.EncodeBytes, with that we could reuse buffer and probably improve performance.

@bokunodev bokunodev changed the title accept and return []byte instead of string qrcode.Encode accept and []byte instead of string Dec 29, 2021
@yeqown
Copy link

yeqown commented Jan 18, 2022

@bokunodev There is a no copy converting trick:

func ToString(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

@bokunodev
Copy link
Author

@bokunodev There is a no copy converting trick:

func ToString(b []byte) string {
	return *(*string)(unsafe.Pointer(&b))
}

where is that code? i couldn't find it with github search.
do i have to implement it?

i have made a working prototype, can i submit a pull request?

@yeqown
Copy link

yeqown commented Jan 19, 2022

it's just a coding trick in go, you should use it to convert your []byte to string, and pass the string variable to QRcode library's API.

by the way, It is not necessary to care about the performance loss of converting between []byte and string at the most time except your application is highly realtime

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