Go(golang) - 方法指针总是按源定义递增吗?

时间:2016-01-03 13:15:02

标签: pointers go

当在Go中定义方法时,指针总是按照它们在源中定义的确切顺序递增 - 或者是否可以重新分配较低的指针空间?

例如 - methods现在是A,Z,D,B的保证顺序,无论是go版本还是拱形?

package main

import (
    "fmt"
    "reflect"
    "sort"
)

type t struct{}

func (a *t) A() {}
func (a *t) Z() {}
func (a *t) D() {}
func (a *t) B() {}

type addr struct {
    Addr   uintptr
    Method string
}

type addrList []addr

func (a addrList) Len() int {
    return len(a)
}

func (a addrList) Less(i, j int) bool {
    return a[i].Addr < a[j].Addr
}
func (a addrList) Swap(i, j int) {
    a[i], a[j] = a[j], a[i]
}

func main() {

    methods := addrList{}
    fooType := reflect.TypeOf(&t{})

    for i := 0; i < fooType.NumMethod(); i++ {
        method := fooType.Method(i)
        methods = append(methods, addr{method.Func.Pointer(), method.Name})
    }

    sort.Sort(methods)
    fmt.Println(methods)
}

1 个答案:

答案 0 :(得分:4)

  

The Go Programming Language Specification

     

这是Go编程语言的参考手册。

订单未在Go语言规范中定义,因此订单未定义。这取决于实现。