数据类型的几点知识

默认值

go语言在声明变量时,自动对变量对应的内存区域进行初始化。每个变量会初始化为其类型的默认值:

  • 整型和浮点型变量默认为0;
  • 字符串变量默认为空字符串;
  • 布尔型变量默认为false;
  • 切片、函数、指针变量默认为nil。
package main

import (
	"fmt"
)

func main() {
	var (
		a int
		b float32
		c string
		d bool
		e []float32
		f func() bool
		g struct {
			x int
		}
	)

	fmt.Printf("a:%v\nb:%v\nc:%v\nd:%v\ne:%v, %v\nf:%v\ng:%v\n", a, b, c, d, e, (e == nil), f, g)
}

输出:

a:0
b:0
c:
d:false
e:[], true
f:<nil>
g:{0}

字符:byte和rune的实际类型

byte:uint8
rune:int32

package main

import (
	"fmt"
)

func main() {
	var (
		a byte = 1
		b rune = 1
	)

	fmt.Printf("byte: %T\nrune: %T\n", a, b)
}

输出:

byte: uint8
rune: int32

数值范围

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Printf("int8 range:\n\t%d ~ %d\n", math.MinInt8, math.MaxInt8)
	fmt.Printf("int16 range:\n\t%d ~ %d\n", math.MinInt16, math.MaxInt16)
	fmt.Printf("int32 range:\n\t%d ~ %d\n", math.MinInt32, math.MaxInt32)
	fmt.Printf("int64 range:\n\t%d ~ %d\n\n", math.MinInt64, math.MaxInt64)

	fmt.Printf("uint8 range:\n\t0 ~ %d\n", math.MaxUint8)
	fmt.Printf("uint16 range:\n\t0 ~ %d\n", math.MaxUint16)
	fmt.Printf("uint32 range:\n\t0 ~ %d\n", math.MaxUint32)
	fmt.Printf("uint64 range:\n\t0 ~ %d\n", uint64(math.MaxUint64))
}

输出:

int8 range:
	-128 ~ 127
int16 range:
	-32768 ~ 32767
int32 range:
	-2147483648 ~ 2147483647
int64 range:
	-9223372036854775808 ~ 9223372036854775807

uint8 range:
	0 ~ 255
uint16 range:
	0 ~ 65535
uint32 range:
	0 ~ 4294967295
uint64 range:
	0 ~ 18446744073709551615

为什么对于math.Uint64要特殊处理呢?

因为在源码中这些值均是常量。在Printf中,这个常量被当做interface{},因此编译器并不知道你实际上想用作哪种类型。对于整型常量,默认为int型,所以溢出了。

通过类型转换,相当于告诉编译器你想把这个值当做uint64。

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package math provides basic constants and mathematical functions.
//
// This package does not guarantee bit-identical results across architectures.
package math

// Mathematical constants.
const (
	E   = 2.71828182845904523536028747135266249775724709369995957496696763 // https://oeis.org/A001113
	Pi  = 3.14159265358979323846264338327950288419716939937510582097494459 // https://oeis.org/A000796
	Phi = 1.61803398874989484820458683436563811772030917980576286213544862 // https://oeis.org/A001622

	Sqrt2   = 1.41421356237309504880168872420969807856967187537694807317667974 // https://oeis.org/A002193
	SqrtE   = 1.64872127070012814684865078781416357165377610071014801157507931 // https://oeis.org/A019774
	SqrtPi  = 1.77245385090551602729816748334114518279754945612238712821380779 // https://oeis.org/A002161
	SqrtPhi = 1.27201964951406896425242246173749149171560804184009624861664038 // https://oeis.org/A139339

	Ln2    = 0.693147180559945309417232121458176568075500134360255254120680009 // https://oeis.org/A002162
	Log2E  = 1 / Ln2
	Ln10   = 2.30258509299404568401799145468436420760110148862877297603332790 // https://oeis.org/A002392
	Log10E = 1 / Ln10
)

// Floating-point limit values.
// Max is the largest finite value representable by the type.
// SmallestNonzero is the smallest positive, non-zero value representable by the type.
const (
	MaxFloat32             = 3.40282346638528859811704183484516925440e+38  // 2**127 * (2**24 - 1) / 2**23
	SmallestNonzeroFloat32 = 1.401298464324817070923729583289916131280e-45 // 1 / 2**(127 - 1 + 23)

	MaxFloat64             = 1.797693134862315708145274237317043567981e+308 // 2**1023 * (2**53 - 1) / 2**52
	SmallestNonzeroFloat64 = 4.940656458412465441765687928682213723651e-324 // 1 / 2**(1023 - 1 + 52)
)

// Integer limit values.
const (
	MaxInt8   = 1<<7 - 1
	MinInt8   = -1 << 7
	MaxInt16  = 1<<15 - 1
	MinInt16  = -1 << 15
	MaxInt32  = 1<<31 - 1
	MinInt32  = -1 << 31
	MaxInt64  = 1<<63 - 1
	MinInt64  = -1 << 63
	MaxUint8  = 1<<8 - 1
	MaxUint16 = 1<<16 - 1
	MaxUint32 = 1<<32 - 1
	MaxUint64 = 1<<64 - 1
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值