美文网首页
go笔记 - 类型转换

go笔记 - 类型转换

作者: 陈德华 | 来源:发表于2020-10-09 15:52 被阅读0次

go 存在 4 种类型转换分别为:断言、强制、显式、隐式。

通常说的类型转换是指断言,强制在日常不会使用到、显示是基本的类型转换、隐式使用到但是不会注意到。断言、强制、显式三类在 go 语法描述中均有说明,隐式是在日常使用过程中总结出来。

1、断言通过判断变量是否可以转换成某一个类型

(1)使用断言表达式

文档地址

var s = x.(T) 或 s, ok := x.(T),注意第一种断言失败会直接panic,开发中尽量使用第二种

例:

var x interface{} = 7          // x has dynamic type int and value 7

i, ok := x.(int)                  // i has type int and value 7

if ok {

    // 成功

}

(2)switch

文档地址

例:

switch i := x.(type) {

case nil:

    printString("x is nil")                // type of i is type of x (interface{})

case int:

    printInt(i)                            // type of i is int

case float64:

    printFloat64(i)                        // type of i is float64

default:

}

2、强制类型转换

强制类型转换通过修改变量类型,通常使用unsafe。文档地址

var f float64

bits = *(*uint64)(unsafe.Pointer(&f))

type ptr unsafe.Pointer

bits = *(*uint64)(ptr(&f))

var p ptr = nil

3、显示类型转换

文档地址

注意要满足以下要求:

x 可以分配成 T 类型。

忽略 struct 标签 x 的类型和 T 具有相同的基础类型。

忽略 struct 标记 x 的类型和 T 是未定义类型的指针类型,并且它们的指针基类型具有相同的基础类型。

x 的类型和 T 都是整数或浮点类型。

x 的类型和 T 都是复数类型。

x 的类型是整数或 [] byte 或 [] rune,并且 T 是字符串类型。

x 的类型是字符串,T 类型是 [] byte 或 [] rune。

例:

uint(iota)              // iota value of type uint

float32(2.718281828)    // 2.718281828 of type float32

complex128(1)            // 1.0 + 0.0i of type complex128

float32(0.49999999)      // 0.5 of type float32

float64(-1e-1000)        // 0.0 of type float64

string('x')              // "x" of type string

string(0x266c)          // "♬" of type string

MyString("foo" + "bar")  // "foobar" of type MyString

string([]byte{'a'})      // not a constant: []byte{'a'} is not a constant

(*int)(nil)              // not a constant: nil is not a constant, *int is not a boolean, numeric, or string type

int(1.2)                // illegal: 1.2 cannot be represented as an int

string(65.0)            // illegal: 65.0 is not an integer constant

4、隐式类型转换

5、数字类型,可以保证以下大小对其:

type                                size in bytes

byte, uint8, int8                               1

uint16, int16                                    2

uint32, int32, float32                       4

uint64, int64, float64, complex64    8

complex128                                   16

相关文章

  • go笔记 - 类型转换

    go 存在 4 种类型转换分别为:断言、强制、显式、隐式。 通常说的类型转换是指断言,强制在日常不会使用到、显示是...

  • 03-数据类型转换

    数值类型转换 C语言中存在显式转换和隐式转换 Go语言中只有显式转换 Go语言中数值类型转换注意点 数值类型转换为...

  • Go语言类型转换

    类型转换 Go语言和C语言不一样,Go语言没有隐式转换,只有显示转换 格式和C语言一样 Go语言类型转换被用于转换...

  • Go 语言基础——变量常量的定义

    go语言不支持隐式类型转换,别名和原有类型也不能进行隐式类型转换go语言不支持隐式转换 变量 变量声明 声明变量不...

  • 2-7 Go --- 类型转换

    Go 语言没有隐式类型转换,类型转换都是显示的强制转换。明明白白写清楚需要转换的类型。 强制类型转换只能在两个类型...

  • Go语言类型转换和类型断言

    Go语言的类型转换和类型断言: 类型转换在编译期完成,包括强制转换和隐式转换 类型断言在运行时确定,包括安全类型断...

  • go 类型转换

    interface{}、断言与类型转换 这篇文章里面的interface{}转换,转换示例非常清楚 (https:...

  • go类型转换

    1.最简单的转换 type_name(expression)float跟int可以互转,但是会丢失所有精度。 1....

  • Go类型转换

    1.string转int 2.int转string 3.string转float 4.float转string 5...

  • go类型转换

    在必要以及可行的情况下,一个类型的值可以被转换成另一种类型的值。由于Go语言不存在隐式类型转换,因此所有的类型转换...

网友评论

      本文标题:go笔记 - 类型转换

      本文链接:https://www.haomeiwen.com/subject/apblpktx.html