GO語言"container/ring"包中"Ring.Link"類型的用法及代碼示例。
用法:
func(r *Ring) Link(s *Ring) *Ring
鏈接將環 r 與環 s 連接起來,使得 r.Next() 變為 s 並返回 r.Next() 的原始值。 r 不能為空。
如果 r 和 s 指向同一個環,則鏈接它們會從環中刪除 r 和 s 之間的元素。刪除的元素形成一個子環,結果是對該子環的引用(如果沒有元素被刪除,結果仍然是 r.Next() 的原始值,而不是 nil)。
如果 r 和 s 指向不同的環,則將它們鏈接起來會創建一個環,其中 s 的元素插入到 r 之後。結果指向插入後 s 的最後一個元素之後的元素。
例子:
package main
import (
"container/ring"
"fmt"
)
func main() {
// Create two rings, r and s, of size 2
r := ring.New(2)
s := ring.New(2)
// Get the length of the ring
lr := r.Len()
ls := s.Len()
// Initialize r with 0s
for i := 0; i < lr; i++ {
r.Value = 0
r = r.Next()
}
// Initialize s with 1s
for j := 0; j < ls; j++ {
s.Value = 1
s = s.Next()
}
// Link ring r and ring s
rs := r.Link(s)
// Iterate through the combined ring and print its contents
rs.Do(func(p any) {
fmt.Println(p.(int))
})
}
輸出:
0 0 1 1
相關用法
- GO Ring.Len用法及代碼示例
- GO Ring.Unlink用法及代碼示例
- GO Ring.Move用法及代碼示例
- GO Ring.Prev用法及代碼示例
- GO Ring.Next用法及代碼示例
- GO Ring.Do用法及代碼示例
- GO Regexp.FindString用法及代碼示例
- GO Regexp.FindAllIndex用法及代碼示例
- GO ResponseRecorder用法及代碼示例
- GO ReverseBytes64用法及代碼示例
- GO ReverseBytes16用法及代碼示例
- GO Regexp.ReplaceAllLiteralString用法及代碼示例
- GO Regexp.FindStringSubmatch用法及代碼示例
- GO Rows用法及代碼示例
- GO Regexp.FindAllString用法及代碼示例
- GO ReadMessage用法及代碼示例
- GO Regexp.ExpandString用法及代碼示例
- GO ResponseWriter用法及代碼示例
- GO Regexp.FindAllStringSubmatch用法及代碼示例
- GO Reverse用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Ring.Link。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。