为什么没有在字符串类型上定义字符串函数?
Why are string functions not defined on the string type?
我只是想知道为什么 Google Go 中的字符串函数是在 strings
包中定义的,而不是 string
数据类型本身。他们本可以轻松完成
func (s string) ToUpper() string {
}
代替现在的
func ToUpper(s string) string {
}
在 strings
包中。
我的猜测是,如果您想在扩展 string
(即 type MyString string
)的自定义类型上实现 ToUpper
的自定义版本,您将无法访问内置 ToUpper
不再适用于该类型,但我找不到任何支持。
string
是 predeclared type in the builtin package.
之一
strings
中的那些函数不能被定义为使用预声明类型作为接收者的方法:这将需要 define a type alias(对于基础类型 string
,以便将方法附加到它)。
A method declaration uses a receiver which has a Type,它又 not 包括任何预先声明的类型(bool byte complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr).
或者(在此处完成),使用专用包“strings
”中的函数。
这似乎与类型字符串本身没有字段的事实相一致:它的内容不必 "receive" 方法,它可以简单地由函数使用。
简短的回答是:"To keep the language simple."
Go 作为一种语言只允许在同一包中的类型上定义方法,但是因为 string
(像其他内置类型一样)是在语言本身中实现的,所以无法添加它的方法,而不会使语言/编译器复杂化。
部分原因在于 Go 的设计方式。
有关更多信息,请参阅 Rob Pike(Go 的创建者之一)的 this mail:
Go does not have methods on basic types because the designers of the
language did not want methods defined for basic types, in part because
of the knock-on effect they might have on interfaces. I believe we are
all still comfortable with that decision. Others may feel differently.
-rob
还有this one:
We simply didn't understand what the implications would be; there's
nothing to explain. Go was designed with caution.
In that vein, look at the size of the strings library. Making all that
functionality methods on the basic type would, as Andrew said,
complicate the language. Why complicate the language with such trivial
things when it can be achieved by a library, which is more
maintainable, easier to extend, and more flexible? The language is
much simpler the way things are.
-rob
我只是想知道为什么 Google Go 中的字符串函数是在 strings
包中定义的,而不是 string
数据类型本身。他们本可以轻松完成
func (s string) ToUpper() string {
}
代替现在的
func ToUpper(s string) string {
}
在 strings
包中。
我的猜测是,如果您想在扩展 string
(即 type MyString string
)的自定义类型上实现 ToUpper
的自定义版本,您将无法访问内置 ToUpper
不再适用于该类型,但我找不到任何支持。
string
是 predeclared type in the builtin package.
strings
中的那些函数不能被定义为使用预声明类型作为接收者的方法:这将需要 define a type alias(对于基础类型 string
,以便将方法附加到它)。
A method declaration uses a receiver which has a Type,它又 not 包括任何预先声明的类型(bool byte complex64 complex128 error float32 float64 int int8 int16 int32 int64 rune string uint uint8 uint16 uint32 uint64 uintptr).
或者(在此处完成),使用专用包“strings
”中的函数。
这似乎与类型字符串本身没有字段的事实相一致:它的内容不必 "receive" 方法,它可以简单地由函数使用。
简短的回答是:"To keep the language simple."
Go 作为一种语言只允许在同一包中的类型上定义方法,但是因为 string
(像其他内置类型一样)是在语言本身中实现的,所以无法添加它的方法,而不会使语言/编译器复杂化。
部分原因在于 Go 的设计方式。
有关更多信息,请参阅 Rob Pike(Go 的创建者之一)的 this mail:
Go does not have methods on basic types because the designers of the language did not want methods defined for basic types, in part because of the knock-on effect they might have on interfaces. I believe we are all still comfortable with that decision. Others may feel differently.
-rob
还有this one:
We simply didn't understand what the implications would be; there's nothing to explain. Go was designed with caution.
In that vein, look at the size of the strings library. Making all that functionality methods on the basic type would, as Andrew said, complicate the language. Why complicate the language with such trivial things when it can be achieved by a library, which is more maintainable, easier to extend, and more flexible? The language is much simpler the way things are.
-rob