當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Ring.Move用法及代碼示例

GO語言"container/ring"包中"Ring.Move"類型的用法及代碼示例。

用法:

func(r *Ring) Move(n int) *Ring

Move 在環中向後 (n < 0) 或向前 (n >= 0) 移動 n % r.Len() 個元素並返回該環元素。 r 不能為空。

例子:

package main

import (
	"container/ring"
	"fmt"
)

func main() {
	// Create a new ring of size 5
	r := ring.New(5)

	// Get the length of the ring
	n := r.Len()

	// Initialize the ring with some integer values
	for i := 0; i < n; i++ {
		r.Value = i
		r = r.Next()
	}

	// Move the pointer forward by three steps
	r = r.Move(3)

	// Iterate through the ring and print its contents
	r.Do(func(p any) {
		fmt.Println(p.(int))
	})

}

輸出:

3
4
0
1
2

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Ring.Move。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。