这种类型声明的含义是什么? [英] What is the meaning of this type declaration?

查看:96
本文介绍了这种类型声明的含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上在学习golang(来自.NET),有一件事我不懂这种语言。
有时我会发现这种声明:



<$

  // PublicKey是Ed25519公钥的类型。 
类型PublicKey []字节

完全是什么意思?
是创建一个继承自[]字节的结构吗?

它只是一个别名吗?



我认为golang禁止继承。

解决方案

这是类型声明,更具体地说是一个类型定义。它创建了一个新类型,其中 [] byte 作为其基础类型:


类型定义使用与给定类型相同的基础类型和操作创建一个新的独特类型,并绑定一个标识符。


创建新类型是因为它们可以简化多次使用它们,它们的标识符(它们的名称)可能会在其他上下文中表现出来,最重要的是,您可以为其定义(附加)方法(不能将方法附加到内置类型,也不能附加到其他包中定义的匿名类型或类型) p>

最后一部分(附加方法)很重要,因为即使不使用附加方法,也可以轻松创建和使用接受原始类型作为参数的函数带有方法的类型可以实现列出(规定)这些方法的接口,并且像提及的那样d之前,你不能将方法附加到某些类型,除非你创建一个从它们派生的新类型。



例如,类型 [ ] int 永远不会执行 sort.Interface 需要可排序(由 sort 包),所以新类型 sort.IntSlice (它是类型IntSlice [] int ),因此您可以将 sort.IntSlice 类型的值传递给 sort.Sort() 函数,但不是类型 [] INT 。由于 sort.IntSlice 具有 [] int 作为其基础类型,所以如果您的值为 [] int ,你可以简单地转换 sort.IntSlice 如果你想对它进行排序,就像这个例子一样(在 Go Playground ):

  is:= [] int {1,3,2 } 
sort.Sort(sort.IntSlice(is))
fmt.Println(is)// Prints:[1 2 3]

创建新类型时,不涉及继承。新类型将有0个方法。如果你想要类似继承的功能,你应该检查嵌入(关于结构类型),在这种情况下,嵌入类型也将拥有嵌入类型的方法。


I am actually learning golang (from .NET) and there is one thing I don't understand about this language. Sometimes I find this kind of declaration:

https://github.com/golang/crypto/blob/master/ed25519/ed25519.go

// PublicKey is the type of Ed25519 public keys.
type PublicKey []byte

What does it mean exactly? Is it a creating a struct which inherit from []byte?

Is it just an alias?

I thought golang forbid inheritance.

解决方案

It's a type declaration, more specifically a type definition. It creates a new type, having []byte as its underlying type:

A type definition creates a new, distinct type with the same underlying type and operations as the given type, and binds an identifier to it.

New types are created because they can simplify using them multiple times, their identifier (their name) may be expressive in other contexts, and–most importantly–so that you can define (attach) methods to it (you can't attach methods to built-in types, nor to anonymous types or types defined in other packages).

This last part (attaching methods) is important, because even though instead of attaching methods you could just as easily create and use functions that accept the "original" type as parameter, only types with methods can implement interfaces that list ("prescribe") those methods, and as mentioned earlier, you can't attach methods to certain types unless you create a new type derived from them.

As an example, the type []int will never implement the sort.Interface required to be sortable (by the sort package), so a new type sort.IntSlice is created (which is type IntSlice []int) to which the required methods are attached, so you can pass a value of type sort.IntSlice to the sort.Sort() function but not a value of type []int. Since sort.IntSlice has []int as its underlying type, if you have a value of []int, you can simply convert it to sort.IntSlice if you want to sort it, like in this example (try it on the Go Playground):

is := []int{1,3,2}
sort.Sort(sort.IntSlice(is))
fmt.Println(is) // Prints: [1 2 3]

When you create a new type, there is no "inheritance" involved. The new type will have 0 methods. If you want "inheritance-like" functionality, you should check out embedding (in relation with struct types), in which case the embedder type will also "have" the methods of the embedded type.

这篇关于这种类型声明的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆