继承是Go语言中嵌入结构体的一种特殊情况吗?

huangapple go评论74阅读模式
英文:

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

你所有问题的答案都是“不”。

  1. string嵌入到结构体中基本上是一个无用的嵌入示例,因为string没有可以提升到嵌入类型的方法。

  2. 构造type Txt string定义了一个名为Txt的新类型,其底层类型是string。Txt具有不同的方法集,因为它是一个不同的类型。但由于其底层类型是string,你可以自由地进行类型转换。

  3. type T string不是type S struct { string }的快捷方式,例如你不能这样做t := T{"foo"},只有t := T("foo")可以工作,而对于S来说则相反。

  4. 嵌入与继承完全没有关系。这是两个不同的概念。在Go中,用嵌入来模拟继承注定会失败,因为你无法在Go中实现继承(这是一个通用建议,它是一个非常有用的建议;但某些特定的继承问题可能可以通过嵌入来解决。只要忘记继承,你就会更加快乐)。

  5. 如果两个类型具有共同的数据和方法,嵌入是很有用的,这种情况下嵌入提供了一些很好的语法糖:不需要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!".

  1. Embedding a string into a struct is basically a useless example of embedding as string has no methods which could get promoted to the embedding type.

  2. 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 is string you may freely type convert them.

  3. type T string is not a shortcut for type S struct { string }, e.g. you cannot do
    t := T{"foo"}, only t := T("foo") works and for S it is the other way around.

  4. 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.)

  5. 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 do type 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 是一种原生类型,就像 intuintfloat 等一样。

在内部,它是一种切片。

引用自 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.

huangapple
  • 本文由 发表于 2014年5月6日 19:36:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/23493647.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定