Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 这里是否生成了某种构造函数?_Sorting_Go - Fatal编程技术网

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
    是一个浮点类型,
    x
    在使用IEEE 754四舍五入到偶数规则进行四舍五入后可由类型为
    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
    是一个浮点类型,
    x
    在使用IEEE 754四舍五入到偶数规则进行四舍五入后可由类型为
    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
的表示形式

没有语言机制在指针和指针之间转换 整数。该包在以下位置实现此功能: 受限制的情况

有关更多详细信息,请参阅链接页面