类型转换用于将一种数据类型的变量转换为另外一种类型的变量。

Go语言类型转换基本格式如下:
表达式 T(v) 将值 v 转换为类型 T 。

Go语言各种类型转换及函数的高级用法:
strconv包实现了基本数据类型和其字符串表示的相互转换。

转字节

reflect.TypeOf() 查看类型

字符串转字节

package main

import (
"fmt"
"reflect"
) func main() {
var str string = "oldboy"
result := []byte(str)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

32位整形转字节

package main

import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
)
// reflect.TypeOf() 查看类型 func main() {
var x int32
x =
bytesBuffer := bytes.NewBuffer([]byte{})
binary.Write(bytesBuffer, binary.BigEndian, x)
result := bytesBuffer.Bytes()
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

转字符串

字节转字符串

package main

import (
"fmt"
"reflect"
) func main() {
var b []byte = []byte{, , , , , }
result := string(b)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

整形转字符串

strconv.Itoa(x)

package main

import (
"fmt"
"reflect"
"strconv"
) func main() {
var x int
x =
result := strconv.Itoa(x)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

FormatInt 将 int 型整数 i 转换为字符串形式 
base:进位制(2 进制到 36 进制) 大于 10 进制的数,返回值使用小写字母 ‘a’ 到 ‘z’

func FormatInt(i int64, base int) string

Itoa 相当于 FormatInt(i, 10)

64位整形转字符串

package main

import (
"fmt"
"reflect"
"strconv"
) func main() {
var i int64
i = 0x100
result := strconv.FormatInt(i, )
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

布尔值转字符串

package main

import (
"fmt"
"reflect"
"strconv"
) func main() {
t := strconv.FormatBool(true)
f := strconv.FormatBool(false)
fmt.Printf("t is %v , t type is %v\n", t, reflect.TypeOf(t))
fmt.Printf("f is %v , f type is %v\n", f, reflect.TypeOf(f))
}

浮点数转字符串

strconv.FormatFloat(f,fmt,prec,bitSize)
f:要转换的浮点数
fmt:格式标记(b、e、E、,f、g、G)
prec:精度(数字部分的长度,不包括指数部分)
bitSize:指定浮点类型(:float32、:float64) 格式标记:
‘b’ (-ddddp±ddd,二进制指数)
‘e’ (-d.dddde±dd,十进制指数)
‘E’ (-d.ddddE±dd,十进制指数)
‘f’ (-ddd.dddd,没有指数)
‘g’ (‘e’:大指数,’f’:其它情况)
‘G’ (‘E’:大指数,’f’:其它情况)
package main import (
"fmt"
"reflect"
"strconv"
) func main() {
f := 100.12345678901234567890123456789
result := strconv.FormatFloat(f, 'e', , )
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

转整形

int转int64

package main

import (
"fmt"
"reflect"
) func main() {
var x int =
result := int64(x)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

字节转32位整形

package main

import (
"bytes"
"encoding/binary"
"fmt"
"reflect"
) func main() {
b := []byte{0x00, 0x00, 0x03, 0xe8}
bytesBuffer := bytes.NewBuffer(b) var result int32
binary.Read(bytesBuffer, binary.BigEndian, &result)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

转浮点型

float32转float64

package main

import (
"fmt"
"reflect"
) func main() {
var x float32 =
result := float64(x)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

整型转浮点型

package main

import (
"fmt"
"reflect"
) func main() {
var x int =
result := float32(x)
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

字符串转浮点数

strconv.ParseFloat(str,bitSize)
str:要转换的字符串
bitSize:指定浮点类型(:float32、:float64)
如果 str 是合法的格式,而且接近一个浮点值,
则返回浮点数的四舍五入值(依据 IEEE754 的四舍五入标准)
如果 str 不是合法的格式,则返回“语法错误”
如果转换结果超出 bitSize 范围,则返回“超出范围”
package main import (
"fmt"
"reflect"
"strconv"
) func main() {
var str string = "0.12345678901234567890"
result, _ := strconv.ParseFloat(str, )
fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

转布尔值

字符串转布尔值

ParseBool 将字符串转换为布尔值 
它接受真值:1, t, T, TRUE, true, True 
它接受假值:0, f, F, FALSE, false, False. 
其它任何值都返回一个错误
package main

import (
    "fmt"
    "reflect"
    "strconv"
) func main() {
    result, _ := strconv.ParseBool("1")
    fmt.Printf("result is %v , result type is %v\n", result, reflect.TypeOf(result))
}

注意:Go语言不能将其他类型当 bool 值使用

package main

func main() {
a :=
if a {
}
}

编译错误:

./main.go::: non-bool a (type int) used as if condition

Go语言类型转换的更多相关文章

  1. Go 语言类型转换

    类型转换用于将一种数据类型的变量转换为另外一种类型的变量.Go 语言类型转换基本格式如下: type_name(expression) type_name 为类型,expression 为表达式. 实 ...

  2. GO语言学习(十七)Go 语言类型转换

    Go 语言类型转换 类型转换用于将一种数据类型的变量转换为另外一种类型的变量.Go 语言类型转换基本格式如下: type_name(expression) type_name 为类型,expressi ...

  3. Delphi VS C语言类型转换对照

    Delphi VS C语言类型转换对照   When converting C function prototypes to Pascal equivalent declarations, it's ...

  4. C语言类型转换原理

    C语言类型转换 int a; a=1.23 这里把1.23赋值给a发生了隐式转换,原理如下: int a; float b=3.14; a=b; b赋值给a的过程:首先找一个中间变量是a的类型(该例中 ...

  5. c语言类型转换注意事项

    转载自: http://blog.csdn.net/zhuimengzh/article/details/6728492 1.隐式转换     C在以下四种情况下会进行隐式转换:        1.算 ...

  6. C语言---类型转换

    itoa 功 能:把一整数转换为字符串 用 法:char *itoa(int value, char *string, int radix); 详细解释:itoa是英文integer to array ...

  7. 【揭秘】C语言类型转换时发生了什么?

    ID:技术让梦想更伟大 作者:李肖遥 链接:https://mp.weixin.qq.com/s/ZFf3imVaJgeesuhl1Kn9sQ 在C语言中,数据类型指的是用于声明不同类型的变量或函数的 ...

  8. Go语言|类型转换和类型别名

    类型转换 同类型之间的转换 Go语言中只有强制类型转换,没有隐式类型转换.该语法只能在两个类型之间支持相互转换的时候使用. import "fmt" func main() { v ...

  9. C语言类型转换

    int/float to string/array: C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串,下面列举了各函数的方法及其说明. itoa():将整型值转 ...

随机推荐

  1. CHANGE DETECTION IN ANGULAR 2

    In this article I will talk in depth about the Angular 2 change detection system. HIGH-LEVEL OVERVIE ...

  2. 网络爬虫--requests库中两个重要的对象

    当我们使用resquests.get()时,返回的时response的对象,他包含服务器返回的所有信息,也包含请求的request的信息. 首先: response对象的属性有以下几个, r.stat ...

  3. centos7下载自定义仓库的镜像设置方法

    1.vim /usr/lib/systemd/system/docker.service Description=Docker Application Container Engine Documen ...

  4. 学习C++的50条忠告

    1. 把C++当成一门新的语言学习: 2. 看<Thinking In C++>,不要看<C++变成死相>: 3. 看<The C++ Programming Langu ...

  5. ScreenCapture-HDwik5.0整合教程

    示例下载:http://yunpan.cn/Q9qzFmf6sF57z 1.上传ScreenCapture文件夹 2.上传upload.php文件 2.1修改upload.php路径 3.修改Scre ...

  6. handsontable-utilities

    搜索值 鼠标右键 讲了四个功能:1.row header是否可以右键(rowheader:true):2.删除右键列表的某些值(通过数组定义):3.自定义右键列表和功能(callback,item两个 ...

  7. D3 数据可视化实战 笔记

    学习真是件奇妙的事情.这本书我之前都看过,有些的知识点却完全没有印象. 总结:把用到的知识好好研究:平时可以了解其他技术的基础,把相关的资料和难点记录下来. javascript陷阱 1.变量类型 v ...

  8. spring获取webapplicationcontext,applicationcontext几种方法详解(转载)

    转载自  http://www.blogjava.net/Todd/archive/2010/04/22/295112.html 方法一:在初始化时保存ApplicationContext对象 代码: ...

  9. SpringMvc与Struts2的对比

    目前企业中使用SpringMvc的比例已经远远超过Struts2,那么两者到底有什么区别,是很多初学者比较关注的问题,下面我们就来对SpringMvc和Struts2进行各方面的比较: 1.核心控制器 ...

  10. [label][Apache] VirtualHost

    <VirtualHost *:80>    ServerName   localhost    DocumentRoot "D:\www"</VirtualHos ...