On Sun, Jan 9, 2022 at 4:30 PM jlfo...@berkeley.edu <jlforr...@berkeley.edu>
wrote:

>
>
> On Sunday, January 9, 2022 at 3:07:18 PM UTC-8 bse...@computer.org wrote:
>
>> Note that a with a map[string]string, the code:
>>
>> m[s]=s
>>
>> The contents of the string s are not duplicated, only the string header s
>> is.
>>
>
> I didn't know this. That's very good to know.
>
> What about this:
>
> package main
>
> type word struct {
>   s string
>   refcnt int
> }
>
> var w1 = word {"moby", 1}
>
> func main() {
>         m := make(map[string]word)
>         m["moby"] = w1
> }
>
> This is a little closer to what I'd like to do.
> How many times would the string "moby" be stored?
>

If you write it as follows, the string "moby" will have only one copy:

m[w1.s]=w1

This will only duplicate the string header `s`, not the contents of 's'.


>
> Thanks,
> Jon
>
> --
> 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/6f06bbe8-8d50-46f6-8b28-f4e680ecae43n%40googlegroups.com
> <https://groups.google.com/d/msgid/golang-nuts/6f06bbe8-8d50-46f6-8b28-f4e680ecae43n%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAMV2RqocHbKrYz8-a9p_W86qoQK7wGTc9%3DWKHOUEKHVSo18CwA%40mail.gmail.com.

Reply via email to