doucigua0449 2014-12-12 20:51
浏览 22
已采纳

这里是否生成某种构造函数?

In one of the sorting examples, they use the following code:

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)

}

The line with the Sort is a bit confusing for me:

sort.Sort(ByAge(people))

Does ByAge(people) generate some kind of constructor that copies the array being passed in? I'm not sure I understand how the new type, ByAge, has access to the elements otherwise.

  • 写回答

1条回答 默认 最新

  • dpdkqls6399 2014-12-12 21:09
    关注

    The syntax foo(expr) where foo is a type and expr is a type conversion, as described in the spec:

    Conversions

    Conversions are expressions of the form T(x) where T is a type and x is an expression that can be converted to type T.

    Conversion = Type "(" Expression [ "," ] ")" .
    

    If the type starts with the operator * or <-, or if the type starts with the keyword func and has no result list, it must be parenthesized when necessary to avoid ambiguity:

    *Point(p)        // same as *(Point(p))
    (*Point)(p)      // p is converted to *Point
    <-chan int(c)    // same as <-(chan int(c))
    (<-chan int)(c)  // c is converted to <-chan int
    func()(x)        // function signature func() x
    (func())(x)      // x is converted to func()
    (func() int)(x)  // x is converted to func() int
    func() int(x)    // x is converted to func() int (unambiguous)
    

    A constant value x can be converted to type T in any of these cases:

    • x is representable by a value of type T.
    • x is a floating-point constant, T is a floating-point type, and x is representable by a value of type T after rounding using IEEE 754 round-to-even rules. The constant T(x) is the rounded value.
    • x is an integer constant and T is a string type. The same rule as for non-constant x applies in this case.

    Converting a constant yields a typed constant as result.

    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
    

    A non-constant value x can be converted to type T in any of these cases:

    • x is assignable to T.
    • x's type and Thave identical underlying types.
    • x's type and T are unnamed pointer types and their pointer base types have identical underlying types.
    • x's type and T are both integer or floating point types. x's type and T are both complex types.
    • x is an integer or a slice of bytes or runes and T is a string type.
    • x is a string and T is a slice of bytes or runes.

    Specific rules apply to (non-constant) conversions between numeric types or to and from a string type. These conversions may change the representation of x and incur a run-time cost. All other conversions only change the type but not the representation of x.

    There is no linguistic mechanism to convert between pointers and integers. The package unsafe implements this functionality under restricted circumstances.

    See the linked page for more details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?