Go 类型转换与别名
发布时间:2023-03-15 编辑:jiaochengji.com
教程集为您提供Go 类型转换与别名等资源,欢迎您收藏本站,我们将为您提供最新的Go 类型转换与别名资源
package main
import "fmt"
func main() {
// Go语言中没有隐式转换,都是显式转换。格式 type(var).即类型(变量)
// 但转换只能在兼容类型下才行
// 1、不兼容类型不能转换 bool <=> int
var b = true
fmt.Printf("b = %d\n",b) // 结果: b = %!d(bool=true)
// fmt.Printf("b = %d\n",int(b))
//结果: .\类型转换.go:11:27: cannot convert b (type bool) to type int
// var i int = 1
// b = bool(i) //结果: .\类型转换.go:15:10: cannot convert i (type int) to type bool
// fmt.Println(b)
// 2、兼容类型 byte<=>int
var c = 'a'
fmt.Printf("c:%d\n",int(c))
var i = 65
fmt.Printf("%c\n",i)
// 3、兼容类型 float<=>int
var f = 3.14
fmt.Println(int(f))
fmt.Printf("%T,%f\n",float64(i),float64(i))
// 4、类型别名
// 使用关键字 type 别名 类型名
type bigint int
var big bigint = 64
fmt.Printf("%T,%d\n",big,big)
// 5、多个别名
type(
longint int
float float64
)
var ff float = 3.14
var l longint = 10
fmt.Printf("%T,%f,%T,%d\n",ff,ff,l,l)
}
到此这篇关于“Go 类型转换与别名”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持JQ教程网!
您可能感兴趣的文章:
上一篇:goLang 特殊符号优先级表
下一篇:go get下载速度慢的解决方法
[关闭]