skip2 / go-qrcode

:sparkles: QR Code encoder (Go)
http://go-qrcode.appspot.com
MIT License
2.58k stars 335 forks source link

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

Open bokunodev opened 2 years ago

bokunodev commented 2 years ago

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.

yeqown commented 2 years ago

@bokunodev There is a no copy converting trick:

func ToString(b []byte) string {
    return *(*string)(unsafe.Pointer(&b))
}
bokunodev commented 2 years ago

@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 commented 2 years ago

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