54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package main
|
||
|
||
import (
|
||
"fmt"
|
||
"math"
|
||
"unsafe"
|
||
)
|
||
|
||
func main() {
|
||
// 整数类型
|
||
var maxInt8 int8 = 127
|
||
var minInt8 int8 = -128
|
||
fmt.Printf("int8范围: %d ~ %d\n", minInt8, maxInt8)
|
||
|
||
// 字节与Unicode码点
|
||
var b byte = 0x61 // 十六进制表示字符'a'
|
||
var r rune = '中' // Unicode码点,值为20013
|
||
fmt.Printf("byte('a'): %T=%d(字符:%c)\n", b, b, b) // uint8=97(字符:a)
|
||
fmt.Printf("rune('中'): %T=%d(字符:%c)\n", r, r, r) // int32=20013(字符:中)
|
||
|
||
// uintptr与指针(底层内存操作,谨慎使用)
|
||
var x int = 100
|
||
p := &x // *int类型,存储x的地址
|
||
ptrVal := uintptr(unsafe.Pointer(p)) // 转换为存储地址的数值
|
||
fmt.Printf("x的地址:%p → uintptr:%x\n", p, ptrVal)
|
||
|
||
// 浮点数
|
||
f32 := float32(3.1415926535)
|
||
f64 := 3.141592653589793 // 默认float64
|
||
fmt.Printf("float32: %.6f, float64: %.12f\n", f32, f64)
|
||
|
||
// 复数
|
||
c := complex(3, 4) // 等价于3+4i(complex128)
|
||
fmt.Printf("复数:%v,实部:%v,虚部:%v\n", c, real(c), imag(c)) // (3+4i) 3 4
|
||
|
||
// 复数运算
|
||
var d complex128 = 1 - 2i
|
||
fmt.Printf("c+d: %v, c*d: %v\n", c+d, c*d) // 输出4+2i, 11-2i
|
||
|
||
// 复数相等判断
|
||
if real(c) == real(d) && imag(c) == imag(d) {
|
||
fmt.Println("c与d相等")
|
||
} else {
|
||
fmt.Println("c与d不相等")
|
||
}
|
||
|
||
// 浮点数比较(考虑精度)
|
||
if math.Abs(f64-float64(f32)) < 1e-6 {
|
||
fmt.Println("浮点数近似相等")
|
||
} else {
|
||
fmt.Println("浮点数不相等")
|
||
}
|
||
}
|