go语法专题:信号量,iota,锁原理

参考:

https://blog.csdn.net/weixin_43753680/article/details/114363122(互斥锁原理)(高水平文章)

https://studygolang.com/articles/2192(golang使用iota)

信号和信号量的基本含义

信号:是由用户、系统或者进程发送给目标进程的信息,以通知目标进程某个状态的改变或系统异常。

信号量:信号量是一个特殊的变量,它的本质是计数器,信号量里面记录了临界资源的数目,有多少数目,信号量的值就为多少,进程对其访问都是原子操作(pv操作,p:占用资源,v:释放资源)。它的作用就是,调协进程对共享资源的访问,让一个临界区同一时间只有一个进程在访问它。

所以它们两的区别也就显而易见了,信号是通知进程产生了某个事件,信号量是用来同步进程的(用来调协进程对共享资源的访问的)

iota

1、iota只能在常量的表达式中使用

fmt.Println(iota) 
编译错误: undefined: iota

2、每次 const 出现时,都会让 iota 初始化为0

const a = iota // a=0
const (
  b = iota     //b=0
  c            //c=1
)

3、自定义类型

自增长常量经常包含一个自定义枚举类型,允许你依靠编译器完成自增设置。

type Stereotype int

const (
    TypicalNoob Stereotype = iota // 0
    TypicalHipster                // 1
    TypicalUnixWizard             // 2
    TypicalStartupFounder         // 3
)

4、可跳过的值

我们可以使用下划线跳过不想要的值。

type AudioOutput int

const (
    OutMute AudioOutput = iota // 0
    OutMono                    // 1
    OutStereo                  // 2
    _
    _
    OutSurround                // 5
)

5、位掩码表达式

type Allergen int

const (
    IgEggs Allergen = 1 << iota // 1 << 0 which is 00000001
    IgChocolate                         // 1 << 1 which is 00000010
    IgNuts                              // 1 << 2 which is 00000100
    IgStrawberries                      // 1 << 3 which is 00001000
    IgShellfish                         // 1 << 4 which is 00010000
)

fmt.Println(IgEggs | IgChocolate | IgShellfish)

// output:
// 19

6、定义数量级

type ByteSize float64

const (
    _           = iota                   // ignore first value by assigning to blank identifier
    KB ByteSize = 1 << (10 * iota) // 1 << (10*1)
    MB                                   // 1 << (10*2)
    GB                                   // 1 << (10*3)
    TB                                   // 1 << (10*4)
    PB                                   // 1 << (10*5)
    EB                                   // 1 << (10*6)
    ZB                                   // 1 << (10*7)
    YB                                   // 1 << (10*8)
)

7、定义在一行的情况

const (
    Apple, Banana = iota + 1, iota + 2
    Cherimoya, Durian
    Elderberry, Fig
)
//iota 在下一行增长,而不是立即取得它的引用。
// Apple: 1
// Banana: 2
// Cherimoya: 2
// Durian: 3
// Elderberry: 3
// Fig: 4

8、中间插队

const (
    i = iota
    j = 3.14
    k = iota
    l
)
//那么打印出来的结果是 i=0,j=3.14,k=2,l=3