英文:
Is inheritance a special case of embedded struct in Go?
问题
在Go语言中,type Txt string
是否只是type Txt struct {string}
的简写形式?
英文:
In Go, is type Txt string
simply a shortcut for type Txt struct {string}
?
答案1
得分: 7
你所有问题的答案都是“不”。
-
将
string
嵌入到结构体中基本上是一个无用的嵌入示例,因为string
没有可以提升到嵌入类型的方法。 -
构造
type Txt string
定义了一个名为Txt的新类型,其底层类型是string。Txt
具有不同的方法集,因为它是一个不同的类型。但由于其底层类型是string
,你可以自由地进行类型转换。 -
type T string
不是type S struct { string }
的快捷方式,例如你不能这样做t := T{"foo"}
,只有t := T("foo")
可以工作,而对于S
来说则相反。 -
嵌入与继承完全没有关系。这是两个不同的概念。在Go中,用嵌入来模拟继承注定会失败,因为你无法在Go中实现继承(这是一个通用建议,它是一个非常有用的建议;但某些特定的继承问题可能可以通过嵌入来解决。只要忘记继承,你就会更加快乐)。
-
如果两个类型具有共同的数据和方法,嵌入是很有用的,这种情况下嵌入提供了一些很好的语法糖:不需要
type T struct { c common; r rest }; t := T{...}; t.common.method()
,你可以使用type T struct { common; r rest }; t := T{...}; t.method()
,这样节省了打字的时间,但基本上是相同的代码。testing
包中包含了一些很好的示例。
英文:
The answers to all your questions is a loud "No!".
-
Embedding a
string
into a struct is basically a useless example of embedding asstring
has no methods which could get promoted to the embedding type. -
The construct
type Txt string
defines a new type named Txt and the underlying type is a string.Txt
has different method set as it is a different type. But as its underlying type isstring
you may freely type convert them. -
type T string
is not a shortcut fortype S struct { string }
, e.g. you cannot do
t := T{"foo"}
, onlyt := T("foo")
works and forS
it is the other way around. -
Embedding has absolutely no relation to inheritance. These are two different things. Mimicking inheritance with embedding is doomed to fail as you cannot do inheritance in Go. (This is a general advice, it is a very useful advice; but some specific inheritance problems might be doable with embedding. Just forget about inheritance and you will be happier.)
-
Embedding is useful if two types have common data and methods in which case embedding provides some nice syntactic sugar: Instead of
type T struct { c common; r rest }; t := T{...}; t.common.method()
you can dotype T struct { common; r rest }; t := T{...}; t.method()
which saves typing but is basically the same code. Package testing contains nice examples.
答案2
得分: 4
No
type Txt string
type StringStruct struct {
string
}
func main() {
var txt Txt = "abc"
fmt.Println(len(txt))
var str string = "abc"
fmt.Println(len(str))
// Uncomment me to get a compiler error!
/* var ss StringStruct = StringStruct{"abc"}
fmt.Println(len(ss)) */
}
你不能在嵌入的结构体上调用len
。你必须调用len(ss.string)
,因此可以肯定地说type Txt string
不是type Txt struct{ string }
的简写。
英文:
No
type Txt string
type StringStruct struct {
string
}
func main() {
var txt Txt = "abc"
fmt.Println(len(txt))
var str string = "abc"
fmt.Println(len(str))
// Uncomment me to get a compiler error!
/* var ss StringStruct = StringStruct{"abc"}
fmt.Println(len(ss)) */
}
You can't call len
on the embedded one. You have to call len(ss.string)
, thus it's safe to say that type Txt string
is not shorthand for type Txt struct{ string }
.
答案3
得分: 2
不,string
是一种原生类型,就像 int
、uint
、float
等一样。
在内部,它是一种切片。
引用自 http://blog.golang.org/strings
> 在 Go 中,字符串实际上是一个只读的字节切片。
>
> 需要明确指出的是,字符串可以包含任意字节。它不需要保存 Unicode 文本、UTF-8 文本或任何其他预定义格式。就字符串的内容而言,它与字节切片完全等效。
英文:
No,string
is a native type, like int
, uint
, float
, etc.
Internally it's a slice of sorts.
Quoting http://blog.golang.org/strings
> In Go, a string is in effect a read-only slice of bytes.
>
> It's important to state right up front that a string holds arbitrary
> bytes. It is not required to hold Unicode text, UTF-8 text, or any
> other predefined format. As far as the content of a string is
> concerned, it is exactly equivalent to a slice of bytes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论