Sorting 这里是否生成了某种构造函数?
在其中一个排序示例中,它们使用以下代码:Sorting 这里是否生成了某种构造函数?,sorting,go,Sorting,Go,在其中一个排序示例中,它们使用以下代码: package main import ( "fmt" "sort" ) type Person struct { Name string Age int } func (p Person) String() string { return fmt.Sprintf("%s: %d", p.Name, p.Age) } // ByAge implements sort.Interface for []Per
package main
import (
"fmt"
"sort"
)
type Person struct {
Name string
Age int
}
func (p Person) String() string {
return fmt.Sprintf("%s: %d", p.Name, p.Age)
}
// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person
func (a ByAge) Len() int { return len(a) }
func (a ByAge) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func main() {
people := []Person{
{"Bob", 31},
{"John", 42},
{"Michael", 17},
{"Jenny", 26},
}
fmt.Println(people)
sort.Sort(ByAge(people))
fmt.Println(people)
}
ByAge(people)是否生成某种构造函数来复制传入的数组?我不确定我是否理解新类型ByAge如何以其他方式访问元素。语法foo(expr)
其中foo
是一种类型,expr
是一种类型转换,如以下所述:
转换
转换是T(x)
形式的表达式,其中T
是一种类型,而x
是可以转换为类型T
的表达式
Conversion = Type "(" Expression [ "," ] ")" .
常数x
可以
在以下任何情况下,转换为类型T
:
可由类型为x
的值表示T
是一个浮点常量,x
是一个浮点类型,T
在使用IEEE 754四舍五入到偶数规则进行四舍五入后可由类型为x
的值表示。常数T
是四舍五入值T(x)
是整数常量,x
是字符串类型。与非常量T
相同的规则适用于这种情况x
uint(iota) // iota value of type uint
float32(2.718281828) // 2.718281828 of type float32
complex128(1) // 1.0 + 0.0i of type complex128
float32(0.49999999) // 0.5 of type float32
string('x') // "x" of type string
string(0x266c) // "♬" of type string
MyString("foo" + "bar") // "foobar" of type MyString
string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
int(1.2) // illegal: 1.2 cannot be represented as an int
string(65.0) // illegal: 65.0 is not an integer constant
非常量值x可以转换为类型T
在上述任何情况下:
可分配给x
T
的类型和x
具有相同的基础类型T
的类型和x
是未命名的指针类型及其指针基类型 具有相同的基础类型T
的类型和x
都是整数或浮点类型T
的类型和x
都是复杂类型T
是一个整数或一段字节或符文,x
是一种字符串类型T
是字符串,x
是字节或符文片段T
x
和的表示形式
产生运行时成本。所有其他转换仅更改类型,但
不是x
的表示形式
没有语言机制在指针和指针之间转换
整数。该包在以下位置实现此功能:
受限制的情况
有关更多详细信息,请参见链接页面。语法foo(expr)
其中foo
是一种类型,expr
是一种类型转换,如下所述:
转换
转换是T(x)
形式的表达式,其中T
是一种类型,而x
是可以转换为类型T
的表达式
Conversion = Type "(" Expression [ "," ] ")" .
常数x
可以
在以下任何情况下,转换为类型T
:
可由类型为x
的值表示T
是一个浮点常量,x
是一个浮点类型,T
在使用IEEE 754四舍五入到偶数规则进行四舍五入后可由类型为x
的值表示。常数T
是四舍五入值T(x)
是整数常量,x
是字符串类型。与非常量T
相同的规则适用于这种情况x
uint(iota) // iota value of type uint
float32(2.718281828) // 2.718281828 of type float32
complex128(1) // 1.0 + 0.0i of type complex128
float32(0.49999999) // 0.5 of type float32
string('x') // "x" of type string
string(0x266c) // "♬" of type string
MyString("foo" + "bar") // "foobar" of type MyString
string([]byte{'a'}) // not a constant: []byte{'a'} is not a constant
(*int)(nil) // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type
int(1.2) // illegal: 1.2 cannot be represented as an int
string(65.0) // illegal: 65.0 is not an integer constant
非常量值x可以转换为类型T
在上述任何情况下:
可分配给x
T
的类型和x
具有相同的基础类型T
的类型和x
是未命名的指针类型及其指针基类型 具有相同的基础类型T
的类型和x
都是整数或浮点类型T
的类型和x
都是复杂类型T
是一个整数或一段字节或符文,x
是一种字符串类型T
是字符串,x
是字节或符文片段T
x
和的表示形式
产生运行时成本。所有其他转换仅更改类型,但
不是x
的表示形式
没有语言机制在指针和指针之间转换
整数。该包在以下位置实现此功能:
受限制的情况
有关更多详细信息,请参阅链接页面