Go基础编程 - 01 -内置类型及函数

内置类型及函数

1、预声明标识符

iota        // (常量)表示连续的无类型整数常量,0索引开始
nil         // (值)表示pointer、channel、func、interface、map、slice类型的零值
true、false // (常量)未类型化的bool值

More info: iota

2、内置类型

2.1、值类型

bool
int, int8, int16, int32(rune), int64,
uint, uint8(byte), uint16, uint32, uint64 
float32, float64
string

complex64, complex128   // 复数类型

int, uint 大小和操作系统位数相关:

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.

uint is a variable sized type, on your 64 bit computer uint is 64 bits wide.

2.2、引用类型(指针类型)

slice   // 切片。与数组区别不固定长度,底层为数组
map     // 字典/映射
chan    // 管道

3、内置函数

Go 语言有一些不需要进行导入就可以使用的内置函数,它们有时可以针对不同的类型进行操作。

make(t Type, size ...IntegerType) Type  // 用来分配并初始化Type,返回Type本身(只能用于引用类型slice, map, channel)
new(Type) *Type                         // 用来分配内存,参数为类型,返回该类型的分配的零值指针(主要用于值类型)
cap(v Type) int                         // 根据v的类型返回v的容量(array、slice、channel、nil)
len(v Type) int                         // 根据v的类型返回v的长度(string、array、slice、map、channel、nil)
append(slice []Type, elems ...Type)     // 追加元素到slice中,返回更新后的slice
copy(dst, src []Type) int               // (浅拷贝)只能用于切片,允许元素区间重叠目;返回复制的元素数min(len(dst), len(src))。
close(c chan<- Type)                    // 用于关闭channel
delete(m map[Type]Type1, key Type)      // 从map中删除key的值
panic(v any)                            // 终止当前线程,抛出panic
recover() any                           // 恢复内置函数允许程序管理一个panic的程序的行为

//(complex、real imag:用于创建和操作复数)
complex(r, i FloatType) ComplexType     // 由两个浮点值构造一个复数值
real(c ComplexType) FloatType           // 返回complex的实部,返回值是对应于c类型的浮点类型。
imag(c ComplexType) FloatType           // 返回complex的虚部
print()println()                      // 底层打印函数,在部署环境中建议使用 fmt包

More info: Go长度与容量

4、内置接口

// any 为 interface{} 的别名
var any = interface{}

// 由所有可比较类型实现的接口
//      booleans, numbers, strings, pointers, channels, 
//      arrays of comparable types, 
//      structs whose fields are all comparable types
// 可比较的接口只能用作类型参数约束,而不能用作变量的类型
type comparable interface{ comparable }

// 表示错误条件的常规接口,nil表示无错误
// 只要实现了Error()函数,返回值为String的都实现了error接口
type error interface {
	Error() string
}

运算符

类型运算符
算术运算符+、-、*、/、、%
关系运算符==、!=、>、≥、<、≤
逻辑运算符&&、||、!
位运算符&、|、^、<<、>>
赋值运算符=、+=、-=、*=、/=、%=、&=、|=、^= 、<<=、>>=

注意:++(自增)、--(自减)在GO语言中是单独的语句,不属于运算符。

位运算符

&:参与运算的两个数各对应的二进制位相与(对应的位都为1,则为1)。
|:参与运算的两个数各对应的二进制位相或(对应的位只要一个为1,则为1)。
^:参与运算的两数各对应的二进制位相异或(对应的位不相同,则为1)。
<<:左移n位就是乘以2的n次方。a << b是把a的各二进制位全部左移b位,高位丢弃,低位补0。
>>:左移n位就是除以2的n次方。a >> b是把a的各二进制位全部右移b位,低位丢弃,高位补0。

package main

import "fmt"

func main() {
    var i int8
    //var i uint8
    i = 8 // 00001000

    fmt.Println(i&3, i&9) // 0 8
    fmt.Println(i|3, i|9) // 11 9
    fmt.Println(i^3, i^9) // 11 1
    fmt.Println(i<<3, i<<4, i<<5) // 64 -128 0  int8最高位为符号位; 若 var i uint8,则结果为 64 128 0
    fmt.Println(i>>3, i>>4, i>>5) // 1 0 0
}

赋值运算符

=:赋值,右侧的值赋值给左侧。
+=:相加后再赋值。
-=:相减后再赋值。
*=:相乘后再赋值。
/=:相除后再赋值。
%=:求余后再赋值。
&=:按位与后再赋值。
|=:按位或后再赋值。
^=:按位异或后再赋值。
<<=:左移后再赋值。
>>=:右移后再赋值。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值