Re: [go-nuts] How to cast a multi-value?

2021-05-19 Thread Brian Candler
On Tuesday, 18 May 2021 at 21:09:18 UTC+1 Amnon wrote:

> My understanding was that a string had a pointer and a length
>

Yes indeed - sorry my brain was disengaged :-)

>

-- 
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/907c7dd7-54f7-4ec4-8cee-4263c114da24n%40googlegroups.com.


Re: [go-nuts] How to cast a multi-value?

2021-05-18 Thread Martin Leiser
Assigning string values to string values or byte[] to byte[] does not 
require additional memory allocation.
But conversion between string and byte[] does so most of the time, because 
the underlying array of a string is immutable, versus the mutable of a 
byte[].

Encapsulating the type cast encapsulates exactly this potentially costly 
operation.
Whether this has a negativ or positive impact on potential compiler 
optimisation in respect to the need of a copy
in this special case, is hard to determine.

Amnon schrieb am Dienstag, 18. Mai 2021 um 22:09:18 UTC+2:

> My understanding was that a string had a pointer and a length,
> whereas a slice had a pointer and a length and a capacity.
>
> https://golang.org/src/reflect/value.go?s=59515:59567#L1973
>
>
> On Tuesday, 18 May 2021 at 20:32:39 UTC+1 Brian Candler wrote:
>
>> Assigning a string value to another variable doesn't double the memory 
>> usage.
>>
>> A value of type string consists of a pointer, a length, and a capacity, 
>> and only these are copied - so you get another copy of the pointer, 
>> pointing to the same data buffer.
>>
>>>

-- 
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/188a70df-4dff-40d7-a2d2-4ea64902aa28n%40googlegroups.com.


Re: [go-nuts] How to cast a multi-value?

2021-05-18 Thread Amnon
My understanding was that a string had a pointer and a length,
whereas a slice had a pointer and a length and a capacity.

https://golang.org/src/reflect/value.go?s=59515:59567#L1973


On Tuesday, 18 May 2021 at 20:32:39 UTC+1 Brian Candler wrote:

> Assigning a string value to another variable doesn't double the memory 
> usage.
>
> A value of type string consists of a pointer, a length, and a capacity, 
> and only these are copied - so you get another copy of the pointer, 
> pointing to the same data buffer.
>
>>

-- 
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/951b1505-e6d7-42d0-b78e-cea207956281n%40googlegroups.com.


Re: [go-nuts] How to cast a multi-value?

2021-05-18 Thread Brian Candler
Assigning a string value to another variable doesn't double the memory 
usage.

A value of type string consists of a pointer, a length, and a capacity, and 
only these are copied - so you get another copy of the pointer, pointing to 
the same data buffer.

>

-- 
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/4c6168df-c5f3-44cd-8191-e6ed4672cbben%40googlegroups.com.


Re: [go-nuts] How to cast a multi-value?

2021-05-18 Thread 'Marc Michael' via golang-nuts
'Keith Randall' via golang-nuts  schrieb am
Di., 18. Mai 2021, 18:25:

> You can use a function call to do the cast:
>
> func byteErr2stringErr(b []byte, e error) (string, error) {
> return string(b), e
> }
> content, err := byteErr2stringErr(os.ReadFile(path))
>
> It's still using additional variables, but they are hidden inside the
> helper function.
> On Sunday, May 16, 2021 at 8:07:24 PM UTC-7 Kurtis Rader wrote:
>
>> On Sun, May 16, 2021 at 7:49 PM 'Marc Michael' via golang-nuts <
>> golan...@googlegroups.com> wrote:
>>
>>> as Go provides multi values I would expect that's possible to cast such
>>> a multi value. Do I see it correctly, that Go does not provide it?
>>>
>>> Example:
>>>
>>> os.ReadFile returns []byte, error.
>>> I want to cast the []byte to a string.
>>>
>>> content, err := os.ReadFile(path)
>>>
>>> Is it possible to cast the []byte directly to a string without using a
>>> second variable?
>>>
>>
>> No, it is not possible to transform the type of one return value in a 
>> multi-valued
>> expression such as a function call. Such syntactic sugar would have limited
>> usefulness and, in my opinion, is likely to decrease readability and thus
>> be a source of bugs. If you find yourself needing to perform such
>> conversions so often that such a feature is useful that suggests the APIs
>> you're using need to be changed.
>>
>
Hello,

thanks for the clarification. I have thought that I had something overseen.

I have thought about introducing a second variable of type string, but this
would lead to doubling of the memory usage, especaly as I don't now really
how much data there is. With the additional function I think this would not
a real problem, as the []byte would go away if the additional function
finishes.

Thanks all for your help. :-)

Kindly regards

>

-- 
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/CAEOc3dUsahY7W3o_6j%3DM6ycRMnpv6XXnHOr0tyXxperzZ%3DB9kg%40mail.gmail.com.


Re: [go-nuts] How to cast a multi-value?

2021-05-18 Thread 'Keith Randall' via golang-nuts
You can use a function call to do the cast:

func byteErr2stringErr(b []byte, e error) (string, error) {
return string(b), e
}
content, err := byteErr2stringErr(os.ReadFile(path))

It's still using additional variables, but they are hidden inside the 
helper function.
On Sunday, May 16, 2021 at 8:07:24 PM UTC-7 Kurtis Rader wrote:

> On Sun, May 16, 2021 at 7:49 PM 'Marc Michael' via golang-nuts <
> golan...@googlegroups.com> wrote:
>
>> as Go provides multi values I would expect that's possible to cast such a 
>> multi value. Do I see it correctly, that Go does not provide it?
>>
>> Example:
>>
>> os.ReadFile returns []byte, error. 
>> I want to cast the []byte to a string.
>>
>> content, err := os.ReadFile(path)
>>
>> Is it possible to cast the []byte directly to a string without using a 
>> second variable?
>>
>
> No, it is not possible to transform the type of one return value in a 
> multi-valued 
> expression such as a function call. Such syntactic sugar would have limited 
> usefulness and, in my opinion, is likely to decrease readability and thus 
> be a source of bugs. If you find yourself needing to perform such 
> conversions so often that such a feature is useful that suggests the APIs 
> you're using need to be changed.
>
> -- 
> Kurtis Rader
> Caretaker of the exceptional canines Junior and Hank
>

-- 
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/eca8c069-5537-43ae-969c-cb334b7d7484n%40googlegroups.com.


Re: [go-nuts] How to cast a multi-value?

2021-05-16 Thread Kurtis Rader
On Sun, May 16, 2021 at 7:49 PM 'Marc Michael' via golang-nuts <
golang-nuts@googlegroups.com> wrote:

> as Go provides multi values I would expect that's possible to cast such a
> multi value. Do I see it correctly, that Go does not provide it?
>
> Example:
>
> os.ReadFile returns []byte, error.
> I want to cast the []byte to a string.
>
> content, err := os.ReadFile(path)
>
> Is it possible to cast the []byte directly to a string without using a
> second variable?
>

No, it is not possible to transform the type of one return value in a
multi-valued
expression such as a function call. Such syntactic sugar would have limited
usefulness and, in my opinion, is likely to decrease readability and thus
be a source of bugs. If you find yourself needing to perform such
conversions so often that such a feature is useful that suggests the APIs
you're using need to be changed.

-- 
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

-- 
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/CABx2%3DD9m0H-boFwAsGXu5wefwqS7cS6mDEZYbvOB99%2BCEqGHwA%40mail.gmail.com.


[go-nuts] How to cast a multi-value?

2021-05-16 Thread 'Marc Michael' via golang-nuts
Hello,

as Go provides multi values I would expect that's possible to cast such a
multi value. Do I see it correctly, that Go does not provide it?

Example:

os.ReadFile returns []byte, error.
I want to cast the []byte to a string.

content, err := os.ReadFile(path)

Is it possible to cast the []byte directly to a string without using a
second variable?

Kindly regards

-- 
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/CAEOc3dWn%3Dn%3DFNaQVhnu56yu9yaoVeJRin_m%2B7DNh8hvqV7Ck%2Bw%40mail.gmail.com.