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

反映以测试值是否为字符串

I'm trying to test if a value coming from the gjson library is a string in the quickest and simplest way possible. I don't want to use a switch type assertion.

if reflect.TypeOf(gjson.Get(input, "name").Value()) != "string" {
    return "Not a string!"  
}

What's wrong with my code?

gjson.Get returns a Result, so you can simply check its Type field:

if gjson.Get(input, "name").Type != gjson.String {
    return "Not a string!"  
}

近期文章

  • 在go中动态初始化数组大小
  • grpc生成的存根线程安全吗?
  • 对MySQL数据库进行动态SQL查询
  • 将动态YAML解组到结构图
  • 从Go中的秒表解析hh:mm:ss时间?
  • 无法获得确切有效的最终网址
  • 依赖接口的golang接口
  • 在Go中通过代理获取URL时出错
  • Golang Web应用程序中的同步
  • Golang sqlite3数据库输出到变量
  • 有没有一种方法可以强制刷新主机?
  • 如何在golang中捕获堆栈溢出错误
  • 如何在Go中计算sha256文件校验和
  • 如何使用结构体指针转换[] byte?
  • 如何在golang中打印未编组的数据?
  • 通过正则表达式从url路径中删除特定路径
  • 使用标签获取Go软件包依赖关系
  • 重写中间件中的内容长度
  • 数据库被锁定(延迟行不起作用)
  • 在Go中生成一定范围内的随机数

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

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

友情链接:代码精华