golang interface 转 string、int、float64
interface{}
interface{} 接口、interface{} 类型很多人都会混淆。interface{} 类型是没有方法的接口。由于没有 implements 关键字,所以说所有的类型都至少实现了 0 个方法,所有类型都实现了空接口。这意味着,如果编写一个函数以 interface{} 值作为参数,那么你可以为该函数提供任何值。例如:
func DoSomething(v interface{}) {
// ...
}
第一种知道是什么类型
如果知道是什么类型的话,就可以直接转
// 假设 v 为 string或int64或float64
func DoSomething(v interface{}) {
string1 := v.(string)
int1 := v.(int64)
float1 := v.(float64)
}
第二种不知道是什么类型
这时候就可以使用类型断言,然后再转为具体类型
func interface2Type(i interface{}) {
switch i.(type) {
case string:
fmt.Println("string", i.(string))
break
case int:
fmt.Println("int", i.(int))
break
case float64:
fmt.Println("float64", i.(float64))
break
}
}
func main() {
interface2Type("niuben")
interface2Type(1122)
interface2Type(11.22)
}
输出
string niuben
int 1122
float64 11.22
golang interface 转 string、int、float64的更多相关文章
- golang interface 转 string,int,float64
func interface2String(inter interface{}) { switch inter.(type) { case string: fmt.Println("stri ...
- Golang Interface 解析
转自 https://zhuanlan.zhihu.com/p/27652856 先看一段代码: 123456789101112 func (x interface{}) { if x == nil ...
- 转载 Golang []byte与string转换的一个误区
Golang []byte与string转换的一个误区 https://www.oyohyee.com/post/Note/golang_byte_to_string/ 2019-08-10 23:4 ...
- QString, string, int, char* 之间相互转换
这三种数据类型在实际运用中经常需要互相转换,那么这里小结下它们之间的转换方法: - Qstring & string Qt中封装的类十分强大,其成员函数数量之多比STD有过之而无不及,许多程序 ...
- string int 转换
int转stringint n = 0;std::stringstream ss;std::string str;ss<<n;ss>>str;string转intstd::st ...
- Java Convert String & Int
To convert a int to string: int num = 123; String str = String.valueOf(num); To convert a string to ...
- C++ char*,const char*,string,int 的相互转换
C++ char*,const char*,string,int 的相互转换 1. string转const char* string s ="abc";const char* ...
- The method makeText(Context, CharSequence, int) in the type Toast is not applicable for the arguments (new View.OnClickListener(){}, String, int)
package comxunfang.button; import android.support.v7.app.ActionBarActivity; import android.os.Bundle ...
- Java不同类型字符转换String/int/Float/////
1.int & String int i=5678;String s=""; int->String: s=i+"";或 s=String.val ...
- string int 类型转换
string int 类型转换 (int) 此方法不适用于将string 转换为int 只能转换数值类型为int 而不能转换引用类型 不会四舍五入 直接去掉小数位 Conver.ToInt() 会 ...
随机推荐
- [转][vue-router] Duplicate named routes definition动态路由addRoutes的踩坑
问题描述: 第一次进入页面,左侧静态路由和动态路由列表均能正常显示,但点击左侧其他路由后浏览器报警告[vue-router] Duplicate named routes definition-,并且 ...
- [转]C# SerialPort串口通信发送接收,处理接收数据完整
废话少说,直接上干货.感兴趣的读者自己去研究代码吧.请见谅. using System; using System.Collections.Generic; using System.IO.Ports ...
- git path
github -> deepin-4090-edd25519-key openl -> deepin-4090-rsa-key gitee -> deepin-4090-dsa-ke ...
- JSON解析的这6种方案,真香!
前言 在 Java 开发中,解析 JSON 是一个非常常见的需求. 不管是和前端交互.调用第三方接口,还是处理配置文件,几乎都绕不开 JSON. 这篇文章总结了6种主流的 JSON 解析方法,希望对你 ...
- .net core 3.x 发布单文件
.翻译自:https://github.com/dotnet/designs/blob/master/accepted/2020/single-file/staging.md NET Core 3.0 ...
- 零基础学习Modbus通信协议
大家好!我是付工. 2012年开始接触Modbus协议,至今已经有10多年了,从开始的懵懂,到后来的顿悟,再到现在的开悟,它始终岿然不动,变化的是我对它的认知和理解. 今天跟大家聊聊关于Modbus协 ...
- C# 获取系统声卡音频数据,并绘制波形
//by wgscd //date:2022/11/7 UI: <Path Stroke="Red" Data="{Binding path}" Rend ...
- 双指针习题:Binary Deque
14.Binary Deque 题面翻译 Binary Deque - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 有多组数据. 每组数据给出 \(n\) 个数,每个数为 \(0\) ...
- Spring Boot进阶教程--注解大全
springboot注解大全 SpringBoot注解就是给代码打上标签的能力.通过引入注解,我们可以简单快速赋予代码生命力,大大提高代码可读性和扩展性.注解本身不具有任何能力,只是一个标签,但是我们 ...
- nginx平台初探-4
模块开发高级篇(30%) 变量(80%) 综述 在Nginx中同一个请求需要在模块之间数据的传递或者说在配置文件里面使用模块动态的数据一般来说都是使用变量,比如在HTTP模块中导出了host/ ...