本文整理汇总了Golang中go/types.StdSizes类的典型用法代码示例。如果您正苦于以下问题:Golang StdSizes类的具体用法?Golang StdSizes怎么用?Golang StdSizes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StdSizes类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: StdSizes
func StdSizes() types.StdSizes {
var std types.StdSizes
// TODO: make dependent on arch
std.WordSize = 8
std.MaxAlign = 8
return std
}
示例2: describeType
func describeType(qpos *queryPos, path []ast.Node) (*describeTypeResult, error) {
var description string
var t types.Type
switch n := path[0].(type) {
case *ast.Ident:
t = qpos.info.TypeOf(n)
switch t := t.(type) {
case *types.Basic:
description = "reference to built-in "
case *types.Named:
isDef := t.Obj().Pos() == n.Pos() // see caveats at isDef above
if isDef {
description = "definition of "
} else if _, ok := qpos.info.ObjectOf(n).(*types.Alias); ok {
description = "alias of "
} else {
description = "reference to "
}
}
case ast.Expr:
t = qpos.info.TypeOf(n)
default:
// Unreachable?
return nil, fmt.Errorf("unexpected AST for type: %T", n)
}
description = description + "type " + qpos.typeString(t)
// Show sizes for structs and named types (it's fairly obvious for others).
switch t.(type) {
case *types.Named, *types.Struct:
szs := types.StdSizes{WordSize: 8, MaxAlign: 8} // assume amd64
description = fmt.Sprintf("%s (size %d, align %d)", description,
szs.Sizeof(t), szs.Alignof(t))
}
return &describeTypeResult{
qpos: qpos,
node: path[0],
description: description,
typ: t,
methods: accessibleMethods(t, qpos.info.Pkg),
fields: accessibleFields(t, qpos.info.Pkg),
}, nil
}
示例3: TestMultipleSizeUse
// Issue 16316
func TestMultipleSizeUse(t *testing.T) {
const src = `
package main
type S struct {
i int
b bool
s string
n int
}
`
ts := findStructType(t, src)
sizes := types.StdSizes{WordSize: 4, MaxAlign: 4}
if got := sizes.Sizeof(ts); got != 20 {
t.Errorf("Sizeof(%v) with WordSize 4 = %d want 20", ts, got)
}
sizes = types.StdSizes{WordSize: 8, MaxAlign: 8}
if got := sizes.Sizeof(ts); got != 40 {
t.Errorf("Sizeof(%v) with WordSize 8 = %d want 40", ts, got)
}
}