本文整理汇总了Golang中reflect.Value.Pointer方法的典型用法代码示例。如果您正苦于以下问题:Golang Value.Pointer方法的具体用法?Golang Value.Pointer怎么用?Golang Value.Pointer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类reflect.Value
的用法示例。
在下文中一共展示了Value.Pointer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: convertValueToString
func convertValueToString(field reflect.Value) (string, bool) {
switch field.Kind() {
case reflect.Float64:
if field.Float() != 0.0 {
return fmt.Sprintf("%f", field.Float()), true
}
case reflect.Bool:
return fmt.Sprintf("%t", field.Bool()), true
case reflect.Int:
if field.Int() != 0 {
return fmt.Sprintf("%d", field.Int()), true
}
case reflect.String:
if field.String() != "" {
return fmt.Sprintf("%s", field.String()), true
}
case reflect.Ptr:
if field.Pointer() != 0 {
field := field.Elem()
if field.Kind() == reflect.Int {
return fmt.Sprintf("%d", field.Int()), true
} else if field.Kind() == reflect.Bool {
return fmt.Sprintf("%t", field.Int()), true
}
}
}
return "", false
}
示例2: validateNewFacade
// validateNewFacade ensures that the facade factory we have has the right
// input and output parameters for being used as a NewFoo function.
func validateNewFacade(funcValue reflect.Value) error {
if !funcValue.IsValid() {
return fmt.Errorf("cannot wrap nil")
}
if funcValue.Kind() != reflect.Func {
return fmt.Errorf("wrong type %q is not a function", funcValue.Kind())
}
funcType := funcValue.Type()
funcName := runtime.FuncForPC(funcValue.Pointer()).Name()
if funcType.NumIn() != 3 || funcType.NumOut() != 2 {
return fmt.Errorf("function %q does not take 3 parameters and return 2",
funcName)
}
facadeType := reflect.TypeOf((*FacadeFactory)(nil)).Elem()
isSame := true
for i := 0; i < 3; i++ {
if funcType.In(i) != facadeType.In(i) {
isSame = false
break
}
}
if funcType.Out(1) != facadeType.Out(1) {
isSame = false
}
if !isSame {
return fmt.Errorf("function %q does not have the signature func (*state.State, *common.Resources, common.Authorizer) (*Type, error)",
funcName)
}
return nil
}
示例3: fmtPointer
func (p *pp) fmtPointer(value reflect.Value, verb rune, goSyntax bool) {
switch verb {
case 'p', 'v', 'b', 'd', 'o', 'x', 'X':
// ok
default:
p.badVerb(verb)
return
}
var u uintptr
switch value.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Slice, reflect.UnsafePointer:
u = value.Pointer()
default:
p.badVerb(verb)
return
}
if goSyntax {
p.add('(')
p.buf.WriteString(value.Type().String())
p.add(')')
p.add('(')
if u == 0 {
p.buf.Write(nilBytes)
} else {
p.fmt0x64(uint64(u), true)
}
p.add(')')
} else if verb == 'v' && u == 0 {
p.buf.Write(nilAngleBytes)
} else {
p.fmt0x64(uint64(u), !p.fmt.sharp)
}
}
示例4: fname
func fname(v reflect.Value) (name string) {
name = runtime.FuncForPC(v.Pointer()).Name()
if i := strings.LastIndex(name, "."); i != -1 {
name = name[i+1:]
}
return name
}
示例5: isMethod
/*
Returns `true` if the given reflect.Value is a struct method.
*/
func isMethod(v reflect.Value) bool {
/*
Based on a hack by Steven Blenkinsop.
http://play.golang.org/p/GD_ZoSpnRe
*/
t := v.Type()
if v.Kind() != reflect.Func {
// Not a function.
return false
}
if t.NumIn() == 0 {
// Methods have at least one parameter.
return false
}
fullname := runtime.FuncForPC(v.Pointer()).Name()
funcname := fullname[strings.LastIndex(fullname, ".")+1:]
// 0 arg is the struct pointer, if it's a method.
method, ok := t.In(0).MethodByName(funcname)
if ok == false {
// No such method with that name.
return false
}
return v.Pointer() == method.Func.Pointer()
}
示例6: formatAtom
// formatAtom formats a value without inspecting its internal structure.
// It is a copy of the the function in gopl.io/ch11/format.
func formatAtom(v reflect.Value) string {
switch v.Kind() {
case reflect.Invalid:
return "invalid"
case reflect.Int, reflect.Int8, reflect.Int16,
reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10)
case reflect.Uint, reflect.Uint8, reflect.Uint16,
reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return strconv.FormatUint(v.Uint(), 10)
// ...floating-point and complex cases omitted for brevity...
case reflect.Bool:
if v.Bool() {
return "true"
}
return "false"
case reflect.String:
return strconv.Quote(v.String())
case reflect.Chan, reflect.Func, reflect.Ptr,
reflect.Slice, reflect.Map:
return v.Type().String() + " 0x" +
strconv.FormatUint(uint64(v.Pointer()), 16)
default: // reflect.Array, reflect.Struct, reflect.Interface
return v.Type().String() + " value"
}
}
示例7: formatValue
func formatValue(value reflect.Value, indentation uint) string {
if indentation > MaxDepth {
return "..."
}
if isNilValue(value) {
return "nil"
}
if UseStringerRepresentation {
if value.CanInterface() {
obj := value.Interface()
switch x := obj.(type) {
case fmt.GoStringer:
return x.GoString()
case fmt.Stringer:
return x.String()
}
}
}
switch value.Kind() {
case reflect.Bool:
return fmt.Sprintf("%v", value.Bool())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return fmt.Sprintf("%v", value.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return fmt.Sprintf("%v", value.Uint())
case reflect.Uintptr:
return fmt.Sprintf("0x%x", value.Uint())
case reflect.Float32, reflect.Float64:
return fmt.Sprintf("%v", value.Float())
case reflect.Complex64, reflect.Complex128:
return fmt.Sprintf("%v", value.Complex())
case reflect.Chan:
return fmt.Sprintf("0x%x", value.Pointer())
case reflect.Func:
return fmt.Sprintf("0x%x", value.Pointer())
case reflect.Ptr:
return formatValue(value.Elem(), indentation)
case reflect.Slice:
return formatSlice(value, indentation)
case reflect.String:
return formatString(value.String(), indentation)
case reflect.Array:
return formatSlice(value, indentation)
case reflect.Map:
return formatMap(value, indentation)
case reflect.Struct:
return formatStruct(value, indentation)
case reflect.Interface:
return formatValue(value.Elem(), indentation)
default:
if value.CanInterface() {
return fmt.Sprintf("%#v", value.Interface())
} else {
return fmt.Sprintf("%#v", value)
}
}
}
示例8: assign
func (s *Shader) assign(ptr unsafe.Pointer, val reflect.Value, typ reflect.Type, name string) error {
u := s.prog.GetUniformLocation(name)
if u < 0 {
return fmt.Errorf("gfx: unknown uniform variable '%s'", name)
}
if typ.Kind() == reflect.Ptr {
if s.assignPrimitive(unsafe.Pointer(val.Pointer()), typ.Elem(), u) {
return nil
}
} else if s.assignPrimitive(ptr, typ, u) {
return nil
}
iface := val.Interface()
switch iface.(type) {
// special types
case *Sampler2D:
sampler := iface.(*Sampler2D)
texunit := s.texunit(u)
gl.ActiveTexture(gl.TEXTURE0 + gl.GLenum(texunit))
sampler.bind()
u.Uniform1i(texunit)
default:
return fmt.Errorf("gfx: invalid uniform type %v", typ)
}
return nil
}
示例9: nonzero
func nonzero(v reflect.Value) bool {
switch v.Kind() {
case reflect.Bool:
return v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() != 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() != 0
case reflect.Float32, reflect.Float64:
return v.Float() != 0
case reflect.Complex64, reflect.Complex128:
return v.Complex() != complex(0, 0)
case reflect.String:
return v.String() != ""
case reflect.Struct:
for i := 0; i < v.NumField(); i++ {
if nonzero(getField(v, i)) {
return true
}
}
return false
case reflect.Array:
for i := 0; i < v.Len(); i++ {
if nonzero(v.Index(i)) {
return true
}
}
return false
case reflect.Map, reflect.Interface, reflect.Slice, reflect.Ptr, reflect.Chan, reflect.Func:
return !v.IsNil()
case reflect.UnsafePointer:
return v.Pointer() != 0
}
return true
}
示例10: PushGoValue
func (lua *Lua) PushGoValue(value reflect.Value) {
switch t := value.Type(); t.Kind() {
case reflect.Bool:
if value.Bool() {
C.lua_pushboolean(lua.State, C.int(1))
} else {
C.lua_pushboolean(lua.State, C.int(0))
}
case reflect.String:
C.lua_pushstring(lua.State, C.CString(value.String()))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
C.lua_pushnumber(lua.State, C.lua_Number(C.longlong(value.Int())))
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
C.lua_pushnumber(lua.State, C.lua_Number(C.ulonglong(value.Uint())))
case reflect.Float32, reflect.Float64:
C.lua_pushnumber(lua.State, C.lua_Number(C.double(value.Float())))
case reflect.Slice:
length := value.Len()
C.lua_createtable(lua.State, C.int(length), 0)
for i := 0; i < length; i++ {
C.lua_pushnumber(lua.State, C.lua_Number(i+1))
lua.PushGoValue(value.Index(i))
C.lua_settable(lua.State, -3)
}
case reflect.Interface:
lua.PushGoValue(value.Elem())
case reflect.Ptr, reflect.UnsafePointer:
C.lua_pushlightuserdata(lua.State, unsafe.Pointer(value.Pointer()))
default:
lua.Panic("wrong return value %v %v", value, t.Kind())
}
}
示例11: GetFuncValueSourceAsString
// GetFuncValueSourceAsString returns the source of the func value fv.
func GetFuncValueSourceAsString(fv reflect.Value) string {
// Checking the kind catches cases where f was nil, resulting in fv being a zero Value (i.e. invalid kind),
// as well as when fv is non-func.
if fv.Kind() != reflect.Func {
return "kind not func"
}
pc := fv.Pointer()
if pc == 0 {
return "nil"
}
function := runtime.FuncForPC(pc)
if function == nil {
return "nil"
}
file, line := function.FileLine(pc)
var startIndex, endIndex int
{
b, err := ioutil.ReadFile(file)
if err != nil {
return "<file not found>"
}
startIndex, endIndex = getLineStartEndIndicies(b, line-1)
}
fs := token.NewFileSet()
fileAst, err := parser.ParseFile(fs, file, nil, 0*parser.ParseComments)
if err != nil {
return "<ParseFile failed>"
}
// TODO: Consider using ast.Walk() instead of custom FindFirst()
query := func(i interface{}) bool {
// TODO: Factor-out the unusual overlap check
if f, ok := i.(*ast.FuncLit); ok && ((startIndex <= int(f.Pos())-1 && int(f.Pos())-1 <= endIndex) || (int(f.Pos())-1 <= startIndex && startIndex <= int(f.End())-1)) {
return true
}
return false
}
funcAst := reflectfind.First(fileAst, query)
// If func literal wasn't found, try again looking for func declaration
if funcAst == nil {
query := func(i interface{}) bool {
// TODO: Factor-out the unusual overlap check
if f, ok := i.(*ast.FuncDecl); ok && ((startIndex <= int(f.Pos())-1 && int(f.Pos())-1 <= endIndex) || (int(f.Pos())-1 <= startIndex && startIndex <= int(f.End())-1)) {
return true
}
return false
}
funcAst = reflectfind.First(fileAst, query)
}
if funcAst == nil {
return fmt.Sprintf("<func src not found at %v:%v>", file, line)
}
return printerutil.SprintAst(fs, funcAst)
}
示例12: funcName
func funcName(val reflect.Value) string {
ptr := val.Pointer()
fn := runtime.FuncForPC(ptr)
if fn == nil {
return fmt.Sprintf("unknown function at %p", ptr)
}
return fn.Name()
}
示例13: valueToXpc
// valueToXpc converts a go Value to an xpc object
//
// note that not all the types are supported, but only the subset required for Blued
func valueToXpc(val r.Value) C.xpc_object_t {
if !val.IsValid() {
return nil
}
var xv C.xpc_object_t
switch val.Kind() {
case r.Int, r.Int8, r.Int16, r.Int32, r.Int64:
xv = C.xpc_int64_create(C.int64_t(val.Int()))
case r.Uint, r.Uint8, r.Uint16, r.Uint32:
xv = C.xpc_int64_create(C.int64_t(val.Uint()))
case r.String:
xv = C.xpc_string_create(C.CString(val.String()))
case r.Map:
xv = C.xpc_dictionary_create(nil, nil, 0)
for _, k := range val.MapKeys() {
v := valueToXpc(val.MapIndex(k))
C.xpc_dictionary_set_value(xv, C.CString(k.String()), v)
if v != nil {
C.xpc_release(v)
}
}
case r.Array, r.Slice:
if val.Type() == TYPE_OF_UUID {
// array of bytes
var uuid [16]byte
r.Copy(r.ValueOf(uuid[:]), val)
xv = C.xpc_uuid_create(C.ptr_to_uuid(unsafe.Pointer(&uuid[0])))
} else if val.Type() == TYPE_OF_BYTES {
// slice of bytes
xv = C.xpc_data_create(unsafe.Pointer(val.Pointer()), C.size_t(val.Len()))
} else {
xv = C.xpc_array_create(nil, 0)
l := val.Len()
for i := 0; i < l; i++ {
v := valueToXpc(val.Index(i))
C.xpc_array_append_value(xv, v)
if v != nil {
C.xpc_release(v)
}
}
}
case r.Interface, r.Ptr:
xv = valueToXpc(val.Elem())
default:
log.Fatalf("unsupported %#v", val.String())
}
return xv
}
示例14: FindMethod
func FindMethod(recvType reflect.Type, funcVal reflect.Value) *reflect.Method {
for i := 0; i < recvType.NumMethod(); i++ {
method := recvType.Method(i)
if method.Func.Pointer() == funcVal.Pointer() {
return &method
}
}
return nil
}
示例15: mark
// Mark value on top of the stack as visited using the registry index.
func (v *visitor) mark(val reflect.Value) {
ptr := val.Pointer()
v.L.RawGeti(lua.LUA_REGISTRYINDEX, v.index)
// Copy value on top.
v.L.PushValue(-2)
// Set value to table.
v.L.RawSeti(-2, int(ptr))
v.L.Pop(1)
}