Overhead of type conversions

136 views
Skip to first unread message

Haddock

unread,
Aug 11, 2025, 12:05:44 PM8/11/25
to golang-nuts
Hello,

in Go there are no functions on built-in types like int, float, string. Let's say I do something like this to have a type mystring that has a function ToUpper:

type mystring string

 func (self *mystring) ToUpper() mystring {

                return mystring(strings.ToUpper(string(*self)))

}

I'm new to Go, have little understanding and knowledge about it. So I'd like to ask what the cost is in that kind of helper functions and the involved conversions on things like memory load, added CPU time, etc. 


Thank you, Haddock

Ian Lance Taylor

unread,
Aug 11, 2025, 12:13:11 PM8/11/25
to Haddock, golang-nuts
The conversion from "string" to "mystring" has no run time cost. In
general, if you have "type T1 T2", then a conversion between "T1" and
"T2" has no run time cost.

Conversions between different types, such as "string" to "[]byte", can
have a run time cost.

Ian
Reply all
Reply to author
Forward
0 new messages