Re: [go-nuts] Go string and UTF-8

2019-08-20 Thread Jan Mercl
On Tue, Aug 20, 2019 at 3:17 PM Pierre Durand  wrote:
>
> Well, in my case I don't want to convert the []byte to hexadecimal string, 
> because it uses 2x more memory.
> The code contains a huge map where the key is an MD5 hash.

md5 hash is an array type and can be used as a map key directly:
https://golang.org/pkg/crypto/md5/#Sum

Example: https://play.golang.org/p/qp-LFWh2Jln

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAA40n-XHXyf4qEeGEVCR2-86wUWzWTnA2y4myjJBZbaCfcAUdA%40mail.gmail.com.


Re: [go-nuts] Go string and UTF-8

2019-08-20 Thread Pierre Durand
Well, in my case I don't want to convert the []byte to hexadecimal string, 
because it uses 2x more memory.
The code contains a huge map where the key is an MD5 hash.

Please note that I'm not personally working on this.
I was reviewing the code written by a coworker, and I noticed that there 
was a string variable containing "invalid UTF-8 bytes".
It felt very strange to have a string containing invalid text.

So I have another question: since md5.Sum() is returning a [16]byte, is it 
better to use [16]byte as a map key ?
Or should I use a string containing invalid text as a map key ?

Le mardi 20 août 2019 14:01:38 UTC+2, Sam Whited a écrit :
>
>
>
> On August 20, 2019 11:50:54 AM UTC, Rob Pike  > wrote: 
> >Printf can print hexadecimal just fine. Never understood the point of 
> >encoding/hex. 
>
> I always thought that the C style format strings were unreadable and the 
> hex package methods were much clearer, personally. 
>
> —Sam 
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4d600bee-5aae-454e-8419-cf6560af37bb%40googlegroups.com.


Re: [go-nuts] Go string and UTF-8

2019-08-20 Thread Sam Whited



On August 20, 2019 11:50:54 AM UTC, Rob Pike  wrote:
>Printf can print hexadecimal just fine. Never understood the point of
>encoding/hex.

I always thought that the C style format strings were unreadable and the hex 
package methods were much clearer, personally.

—Sam

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/4D22ED0E-1F1F-420B-8C26-3EF35F04EDCA%40samwhited.com.


Re: [go-nuts] Go string and UTF-8

2019-08-20 Thread Rob Pike
Printf can print hexadecimal just fine. Never understood the point of
encoding/hex.

Meanwhile, for questions about strings, see blog.golang.org/strings.

-rob


On Tue, Aug 20, 2019 at 9:00 PM Sam Whited  wrote:

> I personally wouldn't do this. If you're going to incur the overhead of a
> heap allocation, might as well incur a bit more and encode the hash, eg.
> using hex.EncodeToString [1], just so that you don't forget and try to
> print or decode the string as utf8 later.
>
> —Sam
>
> [1]: https://godoc.org/encoding/hex#EncodeToString
>
> On August 20, 2019 8:12:20 AM UTC, Pierre Durand 
> wrote:
> >I know that by convention Go string contain UTF-8 encoded text.
> >
> >Is it recommended/a good practice to store invalid bytes in a string ?
> >
> >The use case:
> >- compute a hash => get a []byte
> >- convert the []byte to string (this string is not UTF-8 valid)
> >- use the string as a map key
> >
> >In my case, the hash algorithm is md5.
>
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/42989202-2854-479E-A536-13664BE41946%40samwhited.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOXNBZTKU%3DmSjj%2B9vujYFqphR0Zy%2BMtW0nHW2OayLchEgdvrEA%40mail.gmail.com.


Re: [go-nuts] Go string and UTF-8

2019-08-20 Thread Sam Whited
I personally wouldn't do this. If you're going to incur the overhead of a heap 
allocation, might as well incur a bit more and encode the hash, eg. using 
hex.EncodeToString [1], just so that you don't forget and try to print or 
decode the string as utf8 later.

—Sam

[1]: https://godoc.org/encoding/hex#EncodeToString

On August 20, 2019 8:12:20 AM UTC, Pierre Durand  wrote:
>I know that by convention Go string contain UTF-8 encoded text.
>
>Is it recommended/a good practice to store invalid bytes in a string ?
>
>The use case:
>- compute a hash => get a []byte
>- convert the []byte to string (this string is not UTF-8 valid)
>- use the string as a map key
>
>In my case, the hash algorithm is md5.

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/42989202-2854-479E-A536-13664BE41946%40samwhited.com.