我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

如何检测interface {}是否为指针?

I'm wondering how can you know if the interface is of type pointer.

package main

import "fmt"
import "reflect"

type str struct {
    a, b string
}

func main() {
    var s str
    x := &s
    t := reflect.TypeOf(interface{}(x))
    fmt.Printf("%v", t.Size())
}

Use a type switch if you already know the type(s):

switch v.(type) {
case *str:
    return "*str"
case str:
    return "str"
}

If you don't, then you can use if reflect.TypeOf(v).Kind() == reflect.Ptr {}

playground

近期文章

  • 字符串中允许多个参数[Go]
  • GoLang:调用nil对象的方法时感到恐慌
  • 创建一个不写入标准输出的用户提示
  • 如何创建包含唯一字符串的数组?
  • 访问结构字段时Golang结构文字和指针之间的区别
  • Go或Rust中的数组数组?
  • 我不能在golang中使用变量进行时间计算吗? [重复]
  • 终止第二个goroutine
  • 在Golang中分配值
  • GO lang异常处理
  • 未命名结构类型的方法
  • 我如何开始使用Go? [关闭]
  • 用Go中的函数创建方法的简单方法是什么?
  • GoLang:在Go中进行类型继承和转换的内存分配
  • golang json marshal将标签转换为utf符号
  • 正则表达式转换 countdown timer 到 time
  • go(golang)中的os.exec不执行$ PATH中的命令吗?
  • 是否可以在golang的同一文件夹中拥有两个软件包名称
  • 如何在golang中使用zlib与c中的zlib合作?
  • 具有多个与单个共享结构进行通信的通道是否是线程安全的?

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华