How to cast a multi-value?

188 views
Skip to first unread message

Marc Michael

unread,
May 16, 2021, 10:49:28 PM5/16/21
to golan...@googlegroups.com
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

Kurtis Rader

unread,
May 16, 2021, 11:07:24 PM5/16/21
to Marc Michael, golang-nuts
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

Keith Randall

unread,
May 18, 2021, 12:24:40 PM5/18/21
to 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.

Marc Michael

unread,
May 18, 2021, 3:21:49 PM5/18/21
to golan...@googlegroups.com
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

Brian Candler

unread,
May 18, 2021, 3:32:39 PM5/18/21
to golang-nuts
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.

Amnon

unread,
May 18, 2021, 4:09:18 PM5/18/21
to golang-nuts
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

Martin Leiser

unread,
May 19, 2021, 2:35:04 AM5/19/21
to golang-nuts
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.

Brian Candler

unread,
May 19, 2021, 4:25:56 AM5/19/21
to golang-nuts
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 :-)
Reply all
Reply to author
Forward
0 new messages