dongzhang4301 2016-01-03 13:15
浏览 38
已采纳

Go(golang)-方法指针是否始终按源定义递增?

When methods are defined in Go, does the pointer always increment in the exact order in which they are defined in source - or is it possible that lower pointer space could be re-allocated?

For example - is methods now in the guaranteed order of A, Z, D, B regardless of go version or arch?

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条回答 默认 最新

  • dongxia9519 2016-01-03 13:29
    关注

    The Go Programming Language Specification

    This is a reference manual for the Go programming language.

    The order is not defined in the Go language specification therefore the order is undefined. It's implementation dependent.

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化