Why are string functions not defined for string type?

I'm just wondering why the string functions in Google Go are defined in the package strings, and not in the type itself string. They could easily do

func (s string) ToUpper() string {
}

instead of the current

func ToUpper(s string) string {
}

in the package strings.

I assume that if you want to implement a custom version ToUpperfor a custom type that extends string(i.e. type MyString string), you no longer have access to the built-in ToUpperon that type, but I cannot find support.

+4
source share
2 answers

Short answer: "To make the language simple."

, , string ( ) , , /.

, - , Go.

. ( Go) :

Go , , , , , . , - . .

-rob

:

, ; . Go .

. , , . , , ? .

-rob

+7

string .

strings : ( string, ).

A , Type, , , (bool byte complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr).

( ), strings '.
, : "" , .

+1

All Articles