Golang中的”type By”是什么意思?

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

Golang : type By in Go?

问题

这是来自Golang.org的内容:
http://golang.org/pkg/sort/

// By是一个“less”函数的类型,它定义了其Planet参数的排序方式。
type By func(p1, p2 *Planet) bool

我从来没有见过这种结构。为什么func在type之后?这里的type是什么意思?

我见过以下结构:

type aaaaaa interface { aaa() string }
type dfdfdf struct { }

但从来没有见过像这样的:

type By func(p1, p2 *Planet) bool

在Go语言中,这是如何实现的?type关键字可以接受除了interface和struct之外的其他东西吗?

谢谢~!

英文:

This is from Golang.org
http://golang.org/pkg/sort/

 // By is the type of a "less" function that defines the ordering of its Planet arguments.
 type By func(p1, p2 *Planet) bool

I've never seen this structure. How come func comes after type? And what is type here?

I've seen the following structures but

type aaaaaa interface { aaa() string }
type dfdfdf struct { } 

Never seen like

type By func(p1, p2 *Planet) bool

How this is possible in Go? type can take other things than interface, struct keywords?

Thanks~!

答案1

得分: 3

type By func(p1, p2 *Planet) bool 是定义一个类型的函数值的示例。

我们可以通过创建一个新的 By 值并使用 fmt.Printf 打印类型来看到。在下面的示例中,我将 Planet 设置为字符串 - 对于示例的目的,类型并不重要。

type.go

package main
import(
  "fmt"
  )

type Planet string
type By func(p1, p2 *Planet) bool

func main() {
  fmt.Printf("The type is '%T'", new(By))
  fmt.Println()
}

输出:

mike@tester:~/Go/src/test$ go run type.go
The type is '*main.By'

编辑:根据 nemo 的评论进行了更新。new 关键字返回一个指向新值的指针。func 不像我错误地认为的那样返回一个函数指针,而是返回一个函数值。

英文:

type By func(p1, p2 *Planet) bool is an example of defining a type from a function value.

We can see that by creating a new By value and printing the type using fmt.Printf. In the example below I stumped out Planet as a string - the type doesn't matter for the purposes of the example.

type.go

package main
import(
  "fmt"
  )

type Planet string
type By func(p1, p2 *Planet) bool

func main() {
  fmt.Printf("The type is '%T'", new(By))
  fmt.Println()
}

Output:

mike@tester:~/Go/src/test$ go run type.go
The type is '*main.By'

EDIT: Updated per nemo's comment. The new keyword returns a pointer to the new value. func does not return a function pointer like I had incorrectly thought but instead returns a function value.

答案2

得分: 0

在Go语言中,你可以使用任何基本类型(包括其他用户定义的类型)来定义一个新类型。

例如,如果你定义了一个新类型File:

type File struct {}

并且为它定义了一些方法:

func (f *File) Close() { ... }

func (f *File) Size() { ... }

你可以定义一个名为SpecialFile的新类型:

type SpecialFile File

并在它上面定义自己不同的方法:

func (f *SpecialFile) Close() { (*File)(f).Close() }

需要注意的重要一点是,SpecialFile类型并没有Size方法,即使它的基本类型是File。你必须将其转换为*File类型才能调用Size方法。

即使你不拥有这些类型,甚至它们不在同一个包中,你也可以这样做。

英文:

You can define a new type in go with any base type including another user defined type.

For instance if you define a new type File

type File struct {}

with some methods

func (f *File) Close() { ... }

func (f *File) Size() { ... }

You could then define a new type called:

type SpecialFile File

and define your own different methods on it.

func (f *SpecialFile) Close() { (*File)(f).Close() }

The important thing to note is the SpecialFile type doesn't have a Size method on it even though it's base type is a File. You have to cast it to a *File in order to call the Size method.

You can do this for types you don't even own if you want that aren't even in the same package.

huangapple
  • 本文由 发表于 2013年10月12日 12:10:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/19330426.html
匿名

发表评论

匿名网友

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

确定