基础类型
bool
int8 byte int16 int uint uintptr
float32 float64
complex64 complex128(复数)
string
rune(字符)
error(错误)
bool
true/false
整型
int8 1Byte uint8 1Byte
int16 2Byte uint16 2Byte
int32 4Byte uint32 4Byte
int64 8Byte uint64 8Byte
int、
uint: 与平台有关
uintptr: 32位平台下4Byte,64位平台下8Byte
1、推荐使用int/uint/uintptr
2、int与int32是两种类型,不能自动类型转换
整型变量声明,默认int类型
运算符
+ - * / % : 同C
> < == >= <= != : 同C
类型不同的整型不能直接比较
<< >> ^ & | :同C,左移、右移、异或、位与、位或
^x : 位反,加负号再减1(C中为~x)
浮点型
float32: 同C的float
float64: 同C的double,
默认的浮点类型
1、不同类型的浮点数不能直接进行运算,需要转换成相同的类型
2、浮点数直接比较,可能导致不稳定的结果(未测试出来)
使用数学运算的方式进行比较
return math.Abs(f1-f2) < 0.00001
复数
由两个浮点数组成,分别为实部、虚部
complex64 使用float32分别存储实部、虚部
complex128 使用float64分别存储实部、虚部,默认的处理方式
赋值
var cpx1 = 23 + 3i
cpx2 := complex(23, 3)
分别取实部/虚部
var fc1 =
real(cpx1)
fc2 :=
imag(cpx2)
其他函数
文档:
math/cmplx
string
var str string = "hello world"
1、可以通过下标获取字符,但不能修改,
c1 := str[0]
下标从0开始
2、获取字符串长度 lengh :=
len(str)
3、字符串拼接:str = str1 + str2
其他函数
文档:
strings
rune
字符类型,表示单个Unicode字符
其他函数
文档:unicode包、unicode/utf8包
byte
字符类型,表示UTF-8字符串的单个字节的值
常用