欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Golang并發(fā)編程之Channel詳解

 更新時(shí)間:2023年05月08日 10:05:31   作者:IguoChan  
傳統(tǒng)的并發(fā)編程模型是基于線程和共享內(nèi)存的同步訪問控制的,共享數(shù)據(jù)受鎖的保護(hù),使用線程安全的數(shù)據(jù)結(jié)構(gòu)會(huì)使得這更加容易。本文將詳細(xì)介紹Golang并發(fā)編程中的Channel,,需要的朋友可以參考下

0. 簡(jiǎn)介

傳統(tǒng)的并發(fā)編程模型是基于線程共享內(nèi)存的同步訪問控制的,共享數(shù)據(jù)受鎖的保護(hù),線程將爭(zhēng)奪這些鎖以訪問數(shù)據(jù)。通常而言,使用線程安全的數(shù)據(jù)結(jié)構(gòu)會(huì)使得這更加容易。Go的并發(fā)原語(yǔ)(goroutinechannel)提供了一種優(yōu)雅的方式來(lái)構(gòu)建并發(fā)模型。Go鼓勵(lì)在goroutine之間使用channel來(lái)傳遞數(shù)據(jù),而不是顯式地使用鎖來(lái)限制對(duì)共享數(shù)據(jù)的訪問。

Do not communicate by sharing memory; instead, share memory by communicating.

這就是Go的并發(fā)哲學(xué),它依賴CSP(Communicating Sequential Processes)模型,它經(jīng)常被認(rèn)為是Go在并發(fā)編程上成功的關(guān)鍵因素。

如果說goroutineGo語(yǔ)言程序的并發(fā)體的話,那么channel就是他們之間的通信機(jī)制,前面的系列博客對(duì)goroutine及其調(diào)度機(jī)制進(jìn)行了介紹,本文將介紹一下二者之間的通信機(jī)制——channel。

1. channel數(shù)據(jù)結(jié)構(gòu)

type hchan struct {
   qcount   uint           // total data in the queue
   dataqsiz uint           // size of the circular queue
   buf      unsafe.Pointer // points to an array of dataqsiz elements
   elemsize uint16
   closed   uint32
   elemtype *_type // element type
   sendx    uint   // send index
   recvx    uint   // receive index
   recvq    waitq  // list of recv waiters
   sendq    waitq  // list of send waiters

   // lock protects all fields in hchan, as well as several
   // fields in sudogs blocked on this channel.
   //
   // Do not change another G's status while holding this lock
   // (in particular, do not ready a G), as this can deadlock
   // with stack shrinking.
   lock mutex
}

runtime/chan.go中,channel被定義如上,其中:

  • buf:是有緩存的channel持有的,用來(lái)存儲(chǔ)緩存數(shù)據(jù),收個(gè)循環(huán)鏈表;
  • dataqsiz:上述緩存數(shù)據(jù)的循環(huán)鏈表的最大容量,理解為cap();
  • qcount:上述緩存數(shù)據(jù)的循環(huán)鏈表的長(zhǎng)度,理解為len()
  • recvxsendx:表示上述緩存的接收或者發(fā)送位置;
  • recvqsendq:分別是接收和發(fā)送的goroutine抽象(sudog)隊(duì)列,是個(gè)雙向鏈表;
  • lock:互斥鎖,用來(lái)保證channel數(shù)據(jù)的線程安全。

2. channel創(chuàng)建

func makechan64(t *chantype, size int64) *hchan {
   if int64(int(size)) != size {
      panic(plainError("makechan: size out of range"))
   }

   return makechan(t, int(size))
}

func makechan(t *chantype, size int) *hchan {
   elem := t.elem

   // compiler checks this but be safe.
   if elem.size >= 1<<16 {
      throw("makechan: invalid channel element type")
   }
   if hchanSize%maxAlign != 0 || elem.align > maxAlign {
      throw("makechan: bad alignment")
   }

   mem, overflow := math.MulUintptr(elem.size, uintptr(size))
   if overflow || mem > maxAlloc-hchanSize || size < 0 {
      panic(plainError("makechan: size out of range"))
   }

   // Hchan does not contain pointers interesting for GC when elements stored in buf do not contain pointers.
   // buf points into the same allocation, elemtype is persistent.
   // SudoG's are referenced from their owning thread so they can't be collected.
   // TODO(dvyukov,rlh): Rethink when collector can move allocated objects.
   var c *hchan
   switch {
   case mem == 0:
      // Queue or element size is zero.
      c = (*hchan)(mallocgc(hchanSize, nil, true))
      // Race detector uses this location for synchronization.
      c.buf = c.raceaddr()
   case elem.ptrdata == 0:
      // Elements do not contain pointers.
      // Allocate hchan and buf in one call.
      c = (*hchan)(mallocgc(hchanSize+mem, nil, true))
      c.buf = add(unsafe.Pointer(c), hchanSize)
   default:
      // Elements contain pointers.
      c = new(hchan)
      c.buf = mallocgc(mem, elem, true)
   }

   c.elemsize = uint16(elem.size)
   c.elemtype = elem
   c.dataqsiz = uint(size)
   lockInit(&c.lock, lockRankHchan)

   if debugChan {
      print("makechan: chan=", c, "; elemsize=", elem.size, "; dataqsiz=", size, "\n")
   }
   return c
}

所有的調(diào)用最后都會(huì)走到runtime.makechan函數(shù),函數(shù)做的事情比較簡(jiǎn)單,就是初始化一個(gè)runtime.hchan的對(duì)象,和map一樣,channel對(duì)外就是一個(gè)指針對(duì)象(切片和字符串則不是指針對(duì)象,以切片為例,可以參考鏈接)??梢钥吹剑?/p>

  • 如果當(dāng)前channel沒有緩存,那么就只會(huì)runtime.hchan分配一段空間;
  • 如果當(dāng)前channel中存儲(chǔ)的類型不是指針類型,那么會(huì)為當(dāng)前的runtime.hchan和底層的連續(xù)數(shù)組分配一塊連續(xù)的內(nèi)存空間;
  • 其他情況下,那么則為runtime.hchan和其緩存各自分配一段內(nèi)存;

3. 數(shù)據(jù)發(fā)送

// entry point for c <- x from compiled code
//go:nosplit
func chansend1(c *hchan, elem unsafe.Pointer) {
   chansend(c, elem, true, getcallerpc())
}

channel的數(shù)據(jù)發(fā)送會(huì)調(diào)用runtime.chansend1函數(shù),而該函數(shù)則只是調(diào)用了runtime.chansend函數(shù),該函數(shù)比較長(zhǎng),我們一點(diǎn)一點(diǎn)分析:

3.1 空通道的數(shù)據(jù)發(fā)送

func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
   if c == nil {
      if !block {
         return false
      }
      gopark(nil, nil, waitReasonChanSendNilChan, traceEvGoStop, 2)
      throw("unreachable")
   }

   ...
}

可以看到,如果通道是nil,那么往這個(gè)通道中寫數(shù)據(jù)時(shí):

  • 非阻塞寫會(huì)直接返回(在單channel發(fā)送+default分支的select操作時(shí)會(huì)調(diào)用runtime.selectnbsend函數(shù),從而會(huì)非阻塞寫);
  • 阻塞寫(正常的ch <- v)時(shí)則會(huì)通過gopark函數(shù)讓出CPU調(diào)度權(quán),阻塞此goroutine;

3.2 直接發(fā)送

func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
   ...

   if c.closed != 0 {
      unlock(&c.lock)
      panic(plainError("send on closed channel"))
   }

   if sg := c.recvq.dequeue(); sg != nil {
      // Found a waiting receiver. We pass the value we want to send
      // directly to the receiver, bypassing the channel buffer (if any).
      send(c, sg, ep, func() { unlock(&c.lock) }, 3)
      return true
   }

   ...
}

可以發(fā)現(xiàn),當(dāng)channel被關(guān)閉后再發(fā)送數(shù)據(jù),那么會(huì)導(dǎo)致panic

如果目標(biāo)channel沒有關(guān)閉,且有已經(jīng)處于讀等待的goroutine,那么會(huì)直接從recvq中取出最先陷入等待的goroutine,并通過runtime.send函數(shù)向其發(fā)送數(shù)據(jù):

func send(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
   if raceenabled {
      if c.dataqsiz == 0 {
         racesync(c, sg)
      } else {
         // Pretend we go through the buffer, even though
         // we copy directly. Note that we need to increment
         // the head/tail locations only when raceenabled.
         racenotify(c, c.recvx, nil)
         racenotify(c, c.recvx, sg)
         c.recvx++
         if c.recvx == c.dataqsiz {
            c.recvx = 0
         }
         c.sendx = c.recvx // c.sendx = (c.sendx+1) % c.dataqsiz
      }
   }
   if sg.elem != nil {
      sendDirect(c.elemtype, sg, ep)
      sg.elem = nil
   }
   gp := sg.g
   unlockf()
   gp.param = unsafe.Pointer(sg)
   sg.success = true
   if sg.releasetime != 0 {
      sg.releasetime = cputicks()
   }
   goready(gp, skip+1)
}

可以看到,以上函數(shù)做了兩件事:

  • 調(diào)用sendDirect函數(shù)將發(fā)送的數(shù)據(jù)拷貝到接收協(xié)程的變量所在的地址上;
  • 通過goready函數(shù)喚醒協(xié)程,將其狀態(tài)置為_Grunnable后放置到處理器的隊(duì)列的下一個(gè)待處理goroutine;

3.3 緩存區(qū)

如果沒有已經(jīng)處于讀等待的goroutine,且創(chuàng)建的channel包含緩存,并且緩存還沒有滿,那么會(huì)執(zhí)行以下代碼:

func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
   ...
   if c.qcount < c.dataqsiz {
      // Space is available in the channel buffer. Enqueue the element to send.
      qp := chanbuf(c, c.sendx)
      if raceenabled {
         racenotify(c, c.sendx, nil)
      }
      typedmemmove(c.elemtype, qp, ep)
      c.sendx++
      if c.sendx == c.dataqsiz {
         c.sendx = 0
      }
      c.qcount++
      unlock(&c.lock)
      return true
   }
   ...
}

在這里會(huì)首先通過runtime.chanbuf函數(shù)計(jì)算出下一個(gè)可以存儲(chǔ)的位置,然后通過runtime.typedmemmove將發(fā)送的數(shù)據(jù)拷貝到緩沖區(qū)中并增加sendx索引和qcount計(jì)數(shù)器。等待有接收數(shù)據(jù)的goroutine時(shí)可以直接從緩存中讀取。

3.4 阻塞發(fā)送

如果既沒有等待讀的goroutine,又沒有緩存區(qū)或著緩存區(qū)滿了,那么就會(huì)阻塞發(fā)送數(shù)據(jù):

func chansend(c *hchan, ep unsafe.Pointer, block bool, callerpc uintptr) bool {
   ...

   if !block {
      unlock(&c.lock)
      return false
   }

   // Block on the channel. Some receiver will complete our operation for us.
   gp := getg()
   mysg := acquireSudog()
   mysg.releasetime = 0
   if t0 != 0 {
      mysg.releasetime = -1
   }
   // No stack splits between assigning elem and enqueuing mysg
   // on gp.waiting where copystack can find it.
   mysg.elem = ep
   mysg.waitlink = nil
   mysg.g = gp
   mysg.isSelect = false
   mysg.c = c
   gp.waiting = mysg
   gp.param = nil
   c.sendq.enqueue(mysg)
   // Signal to anyone trying to shrink our stack that we're about
   // to park on a channel. The window between when this G's status
   // changes and when we set gp.activeStackChans is not safe for
   // stack shrinking.
   atomic.Store8(&gp.parkingOnChan, 1)
   gopark(chanparkcommit, unsafe.Pointer(&c.lock), waitReasonChanSend, traceEvGoBlockSend, 2)
   // Ensure the value being sent is kept alive until the
   // receiver copies it out. The sudog has a pointer to the
   // stack object, but sudogs aren't considered as roots of the
   // stack tracer.
   KeepAlive(ep)

   // someone woke us up.
   if mysg != gp.waiting {
      throw("G waiting list is corrupted")
   }
   gp.waiting = nil
   gp.activeStackChans = false
   closed := !mysg.success
   gp.param = nil
   if mysg.releasetime > 0 {
      blockevent(mysg.releasetime-t0, 2)
   }
   mysg.c = nil
   releaseSudog(mysg)
   if closed {
      if c.closed == 0 {
         throw("chansend: spurious wakeup")
      }
      panic(plainError("send on closed channel"))
   }
   return true
}
  • 調(diào)用runtime.getg獲取此時(shí)發(fā)送數(shù)據(jù)的goroutine;
  • 調(diào)用runtime.acquireSudog獲取sudog結(jié)構(gòu)并設(shè)置相關(guān)信息;
  • 將上一步獲取的sudog放到發(fā)送等待隊(duì)列,并且調(diào)用gopark掛起當(dāng)前協(xié)程;
  • 等待有接收數(shù)據(jù)的goroutine到來(lái)后,即喚醒此goroutine,然后繼續(xù)往下走;或者close了此channel,導(dǎo)致后續(xù)的panic。

4. 接收數(shù)據(jù)

4.1 空通道的數(shù)據(jù)接收

func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool) {
   // raceenabled: don't need to check ep, as it is always on the stack
   // or is new memory allocated by reflect.

   if debugChan {
      print("chanrecv: chan=", c, "\n")
   }

   if c == nil {
      if !block {
         return
      }
      gopark(nil, nil, waitReasonChanReceiveNilChan, traceEvGoStop, 2)
      throw("unreachable")
   }

   ...

   lock(&c.lock)

   if c.closed != 0 && c.qcount == 0 {
      if raceenabled {
         raceacquire(c.raceaddr())
      }
      unlock(&c.lock)
      if ep != nil {
         typedmemclr(c.elemtype, ep)
      }
      return true, false
   }

   ...
}

以上是通道接收時(shí)的一部分代碼,可以看到:

  • 和發(fā)送數(shù)據(jù)一樣,如果通道是nil,且非阻塞讀,則會(huì)返回,阻塞讀后則會(huì)掛起;
  • 和發(fā)送數(shù)據(jù)時(shí)不一樣的是,如果是一個(gè)已經(jīng)關(guān)閉的通道,其實(shí)是可讀的,但是讀回的數(shù)據(jù)都是零值+false。

4.2 直接接收

func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool) {
   ...

   if sg := c.sendq.dequeue(); sg != nil {
      // Found a waiting sender. If buffer is size 0, receive value
      // directly from sender. Otherwise, receive from head of queue
      // and add sender's value to the tail of the queue (both map to
      // the same buffer slot because the queue is full).
      recv(c, sg, ep, func() { unlock(&c.lock) }, 3)
      return true, true
   }

   ...
}

當(dāng)channelsendq隊(duì)列中包含處于等待狀態(tài)的goroutine時(shí),會(huì)取出等待的最早的寫數(shù)據(jù)goroutine,然后調(diào)用runtime.recv進(jìn)行發(fā)送:

func recv(c *hchan, sg *sudog, ep unsafe.Pointer, unlockf func(), skip int) {
   if c.dataqsiz == 0 {
      if raceenabled {
         racesync(c, sg)
      }
      if ep != nil {
         // copy data from sender
         recvDirect(c.elemtype, sg, ep)
      }
   } else {
      // Queue is full. Take the item at the
      // head of the queue. Make the sender enqueue
      // its item at the tail of the queue. Since the
      // queue is full, those are both the same slot.
      qp := chanbuf(c, c.recvx)
      if raceenabled {
         racenotify(c, c.recvx, nil)
         racenotify(c, c.recvx, sg)
      }
      // copy data from queue to receiver
      if ep != nil {
         typedmemmove(c.elemtype, ep, qp)
      }
      // copy data from sender to queue
      typedmemmove(c.elemtype, qp, sg.elem)
      c.recvx++
      if c.recvx == c.dataqsiz {
         c.recvx = 0
      }
      c.sendx = c.recvx // c.sendx = (c.sendx+1) % c.dataqsiz
   }
   sg.elem = nil
   gp := sg.g
   unlockf()
   gp.param = unsafe.Pointer(sg)
   sg.success = true
   if sg.releasetime != 0 {
      sg.releasetime = cputicks()
   }
   goready(gp, skip+1)
}

該函數(shù)會(huì)根據(jù)是否存在緩存區(qū)分別處理:

  • 如果不存在緩存區(qū),則調(diào)用runtime.recvDirect函數(shù)直接將發(fā)送goroutine存儲(chǔ)的數(shù)據(jù)拷貝到目標(biāo)內(nèi)存地址中,相當(dāng)于直接從這個(gè)goroutine中取數(shù)據(jù);
  • 如果存在緩存區(qū),那么先將緩存區(qū)中的數(shù)據(jù)拷貝到目標(biāo)內(nèi)存地址中,然后將gp的數(shù)據(jù)拷貝到緩存區(qū)最后,相當(dāng)于先從緩存隊(duì)列頭部取出數(shù)據(jù)給接收goroutine,在從等待發(fā)送goroutine中取出數(shù)據(jù)到緩存隊(duì)列尾部,可以看出,此時(shí)隊(duì)列一定是滿的。

最后無(wú)論哪種情況,都需要調(diào)用goready喚醒gp。

4.3 從緩存區(qū)拿

其實(shí)這里的章節(jié)名描述并不準(zhǔn)確,在4.2中也存在從緩存區(qū)拿數(shù)據(jù)的情況,差別在于:

  • 4.2中緩存隊(duì)列是滿的,且還有發(fā)送阻塞等到的goroutine;
  • 4.3中不存在發(fā)送阻塞等到的goroutine。
func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool) {
   ...

   if c.qcount > 0 {
      // Receive directly from queue
      qp := chanbuf(c, c.recvx)
      if raceenabled {
         racenotify(c, c.recvx, nil)
      }
      if ep != nil {
         typedmemmove(c.elemtype, ep, qp)
      }
      typedmemclr(c.elemtype, qp)
      c.recvx++
      if c.recvx == c.dataqsiz {
         c.recvx = 0
      }
      c.qcount--
      unlock(&c.lock)
      return true, true
   }

   ...
}

和發(fā)送時(shí)一樣,如果緩存區(qū)有數(shù)據(jù),那么從緩存區(qū)拷貝數(shù)據(jù)。

4.4 阻塞接收

func chanrecv(c *hchan, ep unsafe.Pointer, block bool) (selected, received bool) {
   ...

   if !block {
      unlock(&c.lock)
      return false, false
   }

   // no sender available: block on this channel.
   gp := getg()
   mysg := acquireSudog()
   mysg.releasetime = 0
   if t0 != 0 {
      mysg.releasetime = -1
   }
   // No stack splits between assigning elem and enqueuing mysg
   // on gp.waiting where copystack can find it.
   mysg.elem = ep
   mysg.waitlink = nil
   gp.waiting = mysg
   mysg.g = gp
   mysg.isSelect = false
   mysg.c = c
   gp.param = nil
   c.recvq.enqueue(mysg)
   // Signal to anyone trying to shrink our stack that we're about
   // to park on a channel. The window between when this G's status
   // changes and when we set gp.activeStackChans is not safe for
   // stack shrinking.
   atomic.Store8(&gp.parkingOnChan, 1)
   gopark(chanparkcommit, unsafe.Pointer(&c.lock), waitReasonChanReceive, traceEvGoBlockRecv, 2)

   // someone woke us up
   if mysg != gp.waiting {
      throw("G waiting list is corrupted")
   }
   gp.waiting = nil
   gp.activeStackChans = false
   if mysg.releasetime > 0 {
      blockevent(mysg.releasetime-t0, 2)
   }
   success := mysg.success
   gp.param = nil
   mysg.c = nil
   releaseSudog(mysg)
   return true, success
}

和阻塞發(fā)送類似,如果沒有等待發(fā)送的goroutine,且沒有緩存區(qū)或者緩存區(qū)沒有數(shù)據(jù),那這個(gè)時(shí)候就需要將此接收goroutine壓到recvq中,并且gopark掛起,等待喚醒。

5. 關(guān)閉

func closechan(c *hchan) {
   if c == nil {
      panic(plainError("close of nil channel"))
   }

   lock(&c.lock)
   if c.closed != 0 {
      unlock(&c.lock)
      panic(plainError("close of closed channel"))
   }

   if raceenabled {
      callerpc := getcallerpc()
      racewritepc(c.raceaddr(), callerpc, abi.FuncPCABIInternal(closechan))
      racerelease(c.raceaddr())
   }

   c.closed = 1

   var glist gList

   // release all readers
   for {
      sg := c.recvq.dequeue()
      if sg == nil {
         break
      }
      if sg.elem != nil {
         typedmemclr(c.elemtype, sg.elem)
         sg.elem = nil
      }
      if sg.releasetime != 0 {
         sg.releasetime = cputicks()
      }
      gp := sg.g
      gp.param = unsafe.Pointer(sg)
      sg.success = false
      if raceenabled {
         raceacquireg(gp, c.raceaddr())
      }
      glist.push(gp)
   }

   // release all writers (they will panic)
   for {
      sg := c.sendq.dequeue()
      if sg == nil {
         break
      }
      sg.elem = nil
      if sg.releasetime != 0 {
         sg.releasetime = cputicks()
      }
      gp := sg.g
      gp.param = unsafe.Pointer(sg)
      sg.success = false
      if raceenabled {
         raceacquireg(gp, c.raceaddr())
      }
      glist.push(gp)
   }
   unlock(&c.lock)

   // Ready all Gs now that we've dropped the channel lock.
   for !glist.empty() {
      gp := glist.pop()
      gp.schedlink = 0
      goready(gp, 3)
   }
}

關(guān)閉通道的代碼看上去很長(zhǎng),實(shí)際上在處理完一些特殊情況后,就是對(duì)發(fā)送和接收隊(duì)列的數(shù)據(jù)通通使用goready喚醒。

6. 總結(jié)

Go中,雖然極力推崇CSP哲學(xué),推薦大家使用channel實(shí)現(xiàn)共享內(nèi)存的保護(hù),但是:

在幕后,通道使用鎖來(lái)序列化訪問并提供線程安全性。 因此,通過使用通道同步對(duì)內(nèi)存的訪問,你實(shí)際上就是在使用鎖。 被包裝在線程安全隊(duì)列中的鎖。 那么,與僅僅使用標(biāo)準(zhǔn)庫(kù) sync 包中的互斥量相比,Go 的花式鎖又如何呢? 以下數(shù)字是通過使用 Go 的內(nèi)置基準(zhǔn)測(cè)試功能,對(duì)它們的單個(gè)集合連續(xù)調(diào)用 Put 得出的。

`> BenchmarkSimpleSet-8 3000000 391 ns/op`
`> BenchmarkSimpleChannelSet-8 1000000 1699 ns/o`

就我個(gè)人的理解而言:

  • 在進(jìn)行數(shù)據(jù)的傳輸時(shí)使用channel;
  • 在進(jìn)行內(nèi)存數(shù)據(jù)的保護(hù)時(shí)使用sync.Mutex
  • 利用channelselect的特性,實(shí)現(xiàn)類似于Linux epoll的功能。

以上就是Golang并發(fā)編程之Channel詳解的詳細(xì)內(nèi)容,更多關(guān)于Golang Channel的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • 使用Golang?Validator包實(shí)現(xiàn)數(shù)據(jù)驗(yàn)證詳解

    使用Golang?Validator包實(shí)現(xiàn)數(shù)據(jù)驗(yàn)證詳解

    在開發(fā)過程中,數(shù)據(jù)驗(yàn)證是一個(gè)非常重要的環(huán)節(jié),而golang中的Validator包是一個(gè)非常常用和強(qiáng)大的數(shù)據(jù)驗(yàn)證工具,提供了簡(jiǎn)單易用的API和豐富的驗(yàn)證規(guī)則,下面我們就來(lái)看看Validator包的具體使用吧
    2023-12-12
  • 一文掌握Go語(yǔ)言并發(fā)編程必備的Mutex互斥鎖

    一文掌握Go語(yǔ)言并發(fā)編程必備的Mutex互斥鎖

    Go 語(yǔ)言提供了 sync 包,其中包括 Mutex 互斥鎖、RWMutex 讀寫鎖等同步機(jī)制,本篇博客將著重介紹 Mutex 互斥鎖的基本原理,需要的可以參考一下
    2023-04-04
  • Golang RSA生成密鑰、加密、解密、簽名與驗(yàn)簽的實(shí)現(xiàn)

    Golang RSA生成密鑰、加密、解密、簽名與驗(yàn)簽的實(shí)現(xiàn)

    RSA 是最常用的非對(duì)稱加密算法,本文主要介紹了Golang RSA生成密鑰、加密、解密、簽名與驗(yàn)簽的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • golang使用json格式實(shí)現(xiàn)增刪查改的實(shí)現(xiàn)示例

    golang使用json格式實(shí)現(xiàn)增刪查改的實(shí)現(xiàn)示例

    這篇文章主要介紹了golang使用json格式實(shí)現(xiàn)增刪查改的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-05-05
  • Go語(yǔ)言通道之緩沖通道

    Go語(yǔ)言通道之緩沖通道

    這篇文章介紹了Go語(yǔ)言通道之緩沖通道,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • 以Golang為例詳解AST抽象語(yǔ)法樹的原理與實(shí)現(xiàn)

    以Golang為例詳解AST抽象語(yǔ)法樹的原理與實(shí)現(xiàn)

    AST?使用樹狀結(jié)構(gòu)來(lái)表達(dá)編程語(yǔ)言的結(jié)構(gòu),樹中的每一個(gè)節(jié)點(diǎn)都表示源碼中的一個(gè)結(jié)構(gòu),本文將以GO語(yǔ)言為例,為大家介紹一下AST抽象語(yǔ)法樹的原理與實(shí)現(xiàn),希望對(duì)大家有所幫助
    2024-01-01
  • golang實(shí)現(xiàn)分頁(yè)算法實(shí)例代碼

    golang實(shí)現(xiàn)分頁(yè)算法實(shí)例代碼

    這篇文章主要給大家介紹了關(guān)于golang實(shí)現(xiàn)分頁(yè)算法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-09-09
  • Go語(yǔ)言入門教程之Arrays、Slices、Maps、Range操作簡(jiǎn)明總結(jié)

    Go語(yǔ)言入門教程之Arrays、Slices、Maps、Range操作簡(jiǎn)明總結(jié)

    這篇文章主要介紹了Go語(yǔ)言入門教程之Arrays、Slices、Maps、Range操作簡(jiǎn)明總結(jié),本文直接給出操作代碼,同時(shí)對(duì)代碼加上了詳細(xì)注釋,需要的朋友可以參考下
    2014-11-11
  • Go語(yǔ)言學(xué)習(xí)之操作MYSQL實(shí)現(xiàn)CRUD

    Go語(yǔ)言學(xué)習(xí)之操作MYSQL實(shí)現(xiàn)CRUD

    Go官方提供了database包,database包下有sql/driver。該包用來(lái)定義操作數(shù)據(jù)庫(kù)的接口,這保證了無(wú)論使用哪種數(shù)據(jù)庫(kù),操作方式都是相同的。本文就來(lái)和大家聊聊Go語(yǔ)言如何操作MYSQL實(shí)現(xiàn)CRUD,希望對(duì)大家有所幫助
    2023-02-02
  • 詳解golang中的閉包與defer

    詳解golang中的閉包與defer

    閉包一個(gè)函數(shù)與其相關(guān)的引用環(huán)境組合的一個(gè)實(shí)體,其實(shí)可以理解為面向?qū)ο笾蓄愔械膶傩耘c方法,這篇文章主要介紹了golang的閉包與defer,需要的朋友可以參考下
    2022-09-09

最新評(píng)論