package main
import (
	"fmt"
	"github.com/GOgf/gf/v2/container/glist"
)
func main() {
	// Not concurrent-safe in default.
	l := glist.New()
	// Push
	l.PushBack(1)
	l.PushBack(2)
	e := l.PushFront(0)
	// Insert
	l.InsertBefore(e, -1)
	l.InsertAfter(e, "a")
	fmt.Println(l)
	// Pop
	fmt.Println(l.PopFront())
	fmt.Println(l.PopBack())
	fmt.Println(l)
	// All
	fmt.Println(l.FrontAll())
	fmt.Println(l.BackAll())
	// Output:
	// [-1,0,"a",1,2]
	// -1
	// 2
	// [0,"a",1]
	// [0 "a" 1]
	// [1 "a" 0]
}
该示例中我们将通过读锁和写锁遍历一个并发安全的链表,分别通过RLockFunc和LockFunc实现。执行后,输出结果为:

创新互联公司基于成都重庆香港及美国等地区分布式IDC机房数据中心构建的电信大带宽,联通大带宽,移动大带宽,多线BGP大带宽租用,是为众多客户提供专业服务器托管报价,主机托管价格性价比高,为金融证券行业成都西信服务器托管,ai人工智能服务器托管提供bgp线路100M独享,G口带宽及机柜租用的专业成都idc公司。
package main
import (
	"container/list"
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/container/glist"
)
func main() {
	// concurrent-safe list.
	l := glist.NewFrom(garray.NewArrayRange(1, 10, 1).Slice(), true)
	// iterate reading from head.
	l.RLockFunc(func(list *list.List) {
		length := list.Len()
		if length > 0 {
			for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() {
				fmt.Print(e.Value)
			}
		}
	})
	fmt.Println()
	// iterate reading from tail.
	l.RLockFunc(func(list *list.List) {
		length := list.Len()
		if length > 0 {
			for i, e := 0, list.Back(); i < length; i, e = i+1, e.Prev() {
				fmt.Print(e.Value)
			}
		}
	})
    fmt.Println()
    // iterate reading from head using IteratorAsc.
	l.IteratorAsc(func(e *glist.Element) bool {
		fmt.Print(e.Value)
		return true
	})
	fmt.Println()
	// iterate reading from tail using IteratorDesc.
	l.IteratorDesc(func(e *glist.Element) bool {
		fmt.Print(e.Value)
		return true
	})
	fmt.Println()
	// iterate writing from head.
	l.LockFunc(func(list *list.List) {
		length := list.Len()
		if length > 0 {
			for i, e := 0, list.Front(); i < length; i, e = i+1, e.Next() {
				if e.Value == 6 {
					e.Value = "M"
					break
				}
			}
		}
	})
	fmt.Println(l)
	// Output:
	// 12345678910
	// 10987654321
	// 12345678910
	// 10987654321
	// [1,2,3,4,5,"M",7,8,9,10]
}
package main
import (
	"fmt"
	"github.com/gogf/gf/v2/container/glist"
	"github.com/gogf/gf/v2/frame/g"
)
func main() {
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5})
	l.PushBack(6)
	fmt.Println(l)
	l.PushFront(0)
	fmt.Println(l)
	l.PushBacks(g.Slice{7, 8})
	fmt.Println(l)
	l.PushFronts(g.Slice{-1, -2})
	fmt.Println(l)
	l.PushFrontList(glist.NewFrom(g.Slice{"a", "b", "c"}))
	l.PushBackList(glist.NewFrom(g.Slice{"d", "e", "f"}))
	fmt.Println(l)
	// Output:
	// [1,2,3,4,5,6]
	// [0,1,2,3,4,5,6]
	// [0,1,2,3,4,5,6,7,8]
	// [-2,-1,0,1,2,3,4,5,6,7,8]
	// ["a","b","c",-2,-1,0,1,2,3,4,5,6,7,8,"d","e","f"]
}
package main
import (
	"fmt"
	"github.com/gogf/gf/v2/container/glist"
	"github.com/gogf/gf/v2/frame/g"
)
func main() {
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
	fmt.Println(l.PopBack())
	fmt.Println(l.PopBacks(2))
	fmt.Println(l.PopFront())
	fmt.Println(l.PopFronts(2))
    
	fmt.Println(glist.NewFrom(g.Slice{"a", "b", "c", "d"}).PopFrontAll())
	fmt.Println(glist.NewFrom(g.Slice{"a", "b", "c", "d"}).PopBackAll())
	// Output:
	// 9
	// [8 7]
	// 1
	// [2 3]
    // [4,5,6]
	// [a b c d]
	// [d c b a]
}
package main
import (
	"fmt"
	"github.com/gogf/gf/v2/container/glist"
	"github.com/gogf/gf/v2/frame/g"
)
func main() {
	l := glist.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})
	l.MoveToBack(l.Front())
	l.MoveToFront(l.Back().Prev())
	fmt.Println(l)
	// 将2到栈首元素的前面
	l.MoveBefore(l.Front().Next(), l.Front())
	// 将8到栈尾元素的后面
	l.MoveAfter(l.Back().Prev(), l.Back())
	fmt.Println(l)
	// 在栈尾元素前插入新元素
	l.InsertBefore(l.Back(), "a")
	// 在栈首元素后插入新元素
	l.InsertAfter(l.Front(), "b")
	// Output:
	// [9,2,3,4,5,6,7,8,1]
	// [2,9,3,4,5,6,7,1,8]
	// [2,"b",9,3,4,5,6,7,1,"a",8]
}
package main
import (
	"fmt"
	"github.com/gogf/gf/v2/container/glist"
	"github.com/gogf/gf/v2/frame/g"
)
func main() {
	var l glist.List
	l.PushBacks(g.Slice{"a", "b", "c", "d"})
	fmt.Println(l.Join(","))
	// Output:
	// a,b,c,d
}
package main
import (
	"fmt"
	"github.com/gogf/gf/v2/container/glist"
	"github.com/gogf/gf/v2/frame/g"
)
func main() {
	l := glist.NewFrom(g.Slice{0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
	fmt.Println(l.Remove(l.Front()))
	fmt.Println(l)
 	l.Removes([]*glist.Element{l.Front(), l.Front().Next()})
 	fmt.Println(l)
	l.RemoveAll()
	fmt.Println(l)
	// Output:
 	// [0,1,2,3,4,5,6,7,8,9]
	// [1,2,3,4,5,6,7,8,9]
	// [3,4,5,6,7,8,9]
	// []
}
glist容器实现了标准库json数据格式的序列化/反序列化接口。
package main
import (
    "encoding/json"
    "fmt"
    "github.com/gogf/gf/v2/container/glist"
    "github.com/gogf/gf/v2/frame/g"
)
func main() {
    type Student struct {
        Id     int
        Name   string
        Scores *glist.List
    }
    s := Student{
        Id:     1,
        Name:   "john",
        Scores: glist.NewFrom(g.Slice{100, 99, 98}),
    }
    b, _ := json.Marshal(s)
    fmt.Println(string(b))
	// Output:
	// {"Id":1,"Name":"john","Scores":[100,99,98]}
}
package main
import (
    "encoding/json"
    "fmt"
    "github.com/gogf/gf/v2/container/glist"
)
func main() {
    b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
    type Student struct {
        Id     int
        Name   string
        Scores *glist.List
    }
    s := Student{}
    json.Unmarshal(b, &s)
    fmt.Println(s)
	// Output:
	// {1 john [100,99,98]}
}            
                文章题目:创新互联GoFrame教程:GoFrameglist-基本使用
                
                浏览地址:http://www.csdahua.cn/qtweb/news6/275656.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网