Go语言常见数据类型

go语言与c语言类似也有比较多的数据类型,我们可以在控制台输出简单的幂次方的值,在goland中按住ctrl + 鼠标点击需要查看的类,点击进去可以看到常见的数据类型以及对应的数据范围,go语言有一个不方便的点是不同类型的整数或者浮点数之间不能够直接使用比较运算符进行比较,比如变量a属于int32类型的变量,变量b属于int类型的变量,两者不可以使用比较运算符直接比较,需要转化为同一类型的整数才可以比较,一般来说可以使用对应数据类型的函数将一个数字转为对应类型的数字(强制类型转换),例如将类型为int32的整数x转为int类型的数字可以使用res := int(x),转化为哪一种数据类型那么使用对应名字的函数即可,所以不管是我们自己定义的函数还是Go语言内置的函数函数的在传递参数的时候都需要保持类型一致的原则,使用对应名字的函数将其转换为对应类型的数据类型即可,感觉Go语言对于数据类型的规范还是挺严格的。 

package main

import (
	"fmt"
	"math"
)

func main() {
	t := int8(math.Pow(2, 7) - 1)
	fmt.Println(t)
}
package main

import "fmt"

func getMax(a, b int64) int64 {
	if a >= b {
		return a
	}
	return b
}

func main() {
    // 将int类型的变量a通过对应函数int64转换为int64类型的变量
	a, b := int64(10), 12
	// a为int64的整数, b是int整数, 两者不可以直接比较, 将b转为int64类型的整数这样就可以直接比较了
	fmt.Println(getMax(a, int64(b)))
}
// uint8 is the set of all unsigned 8-bit integers.
// Range: 0 through 255.
type uint8 uint8

// uint16 is the set of all unsigned 16-bit integers.
// Range: 0 through 65535.
type uint16 uint16

// uint32 is the set of all unsigned 32-bit integers.
// Range: 0 through 4294967295.
type uint32 uint32

// uint64 is the set of all unsigned 64-bit integers.
// Range: 0 through 18446744073709551615.
type uint64 uint64

// int8 is the set of all signed 8-bit integers.
// Range: -128 through 127.
type int8 int8

// int16 is the set of all signed 16-bit integers.
// Range: -32768 through 32767.
type int16 int16

// int32 is the set of all signed 32-bit integers.
// Range: -2147483648 through 2147483647.
type int32 int32

// int64 is the set of all signed 64-bit integers.
// Range: -9223372036854775808 through 9223372036854775807.
type int64 int64

// float32 is the set of all IEEE-754 32-bit floating-point numbers.
type float32 float32

// float64 is the set of all IEEE-754 64-bit floating-point numbers.
type float64 float64

// complex64 is the set of all complex numbers with float32 real and
// imaginary parts.
type complex64 complex64

// complex128 is the set of all complex numbers with float64 real and
// imaginary parts.
type complex128 complex128

// string is the set of all strings of 8-bit bytes, conventionally but not
// necessarily representing UTF-8-encoded text. A string may be empty, but
// not nil. Values of string type are immutable.
type string string

// int is a signed integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, int32.
type int int

// uint is an unsigned integer type that is at least 32 bits in size. It is a
// distinct type, however, and not an alias for, say, uint32.
type uint uint

// uintptr is an integer type that is large enough to hold the bit pattern of
// any pointer.
type uintptr uintptr

// byte is an alias for uint8 and is equivalent to uint8 in all ways. It is
// used, by convention, to distinguish byte values from 8-bit unsigned
// integer values.
type byte = uint8

// rune is an alias for int32 and is equivalent to int32 in all ways. It is
// used, by convention, to distinguish character values from integer values.
type rune = int32
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值