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

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. [Training Video - 6] [File Reading] Making a Jar file with eclispe, Importing custom jars in SoapUI

    Code example : package com.file.properties; import java.io.FileInputStream; import java.util.Propert ...

  2. 各大主流.Net的IOC框架性能测试比较(转)

    出处:http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html 在上一篇中,我简单介绍了下Autofac的使用,有人希 ...

  3. redhat6.7在线安装postgresql9

    原文:http://wandejun1012.iteye.com/blog/2015777 1.安装postgresql9.0 yum 仓库 rpm -i http://yum.postgresql. ...

  4. ios9出现的问题

    升级后需要注意两个地方  1 在build Settings 搜索bitcode 设置成no 2 在info.plist里添加以下属性  程序中报错:  App Transport Security ...

  5. type="submit" 和type="button"

    今天,小菜鸟又遇到一个问题,当不小心在页面输入框回车一下,结果莫名的页面发出了一个请求. 把问题定位在一个button上,代码是这样写的<button class="btn btn-d ...

  6. Intel Cyclone SoC FPGA介绍

    3.1 Intel Cyclone SoC FPGA介绍 3.1.1 SoC FPGA的基本概念 Intel Cyclone V SoC FPGA是Intel PSG(原Altera)于2013年发布 ...

  7. CodeForces - 589F —(二分+贪心)

    A gourmet came into the banquet hall, where the cooks suggested n dishes for guests. The gourmet kno ...

  8. UI 设计概念介绍

    UI 设计概念介绍 http://www.slideshare.net/tedzhaoxa/ui-and-ue-design-basic

  9. IIS 8 nodejs + iisnode 配置

    最近再Server 2012 + IIS 8 中配置NodeJS 运行环境,具体配置过程就不细说了(随便搜搜一堆),安装完nodejs 和 iisnode 之后,出现一个报错,如下: The iisn ...

  10. Angularjs 分页控件

    实现效果: 实现步骤: 1.分页页面:page.html,页面多余样式,需要自己去除. <div class="row" ng-show="conf.totalIt ...