日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)GoFrame教程:GoFramegarray-基本使用

普通數(shù)組

package main

import (
    "fmt"
    "github.com/GOgf/gf/v2/container/garray"
)


func main () {
    // 創(chuàng)建并發(fā)安全的int類型數(shù)組
    a := garray.NewIntArray(true)

    // 添加數(shù)據(jù)項(xiàng)
    for i := 0; i < 10; i++ {
        a.Append(i)
    }

    // 獲取當(dāng)前數(shù)組長度
    fmt.Println(a.Len())

    // 獲取當(dāng)前數(shù)據(jù)項(xiàng)列表
    fmt.Println(a.Slice())

    // 獲取指定索引項(xiàng)
    fmt.Println(a.Get(6))

    // 在指定索引后插入數(shù)據(jù)項(xiàng)
    a.InsertAfter(9, 11)
    // 在指定索引前插入數(shù)據(jù)項(xiàng)
    a.InsertBefore(10, 10)
    fmt.Println(a.Slice())

    // 修改指定索引的數(shù)據(jù)項(xiàng)
    a.Set(0, 100)
    fmt.Println(a.Slice())

    // 搜索數(shù)據(jù)項(xiàng),返回搜索到的索引位置
    fmt.Println(a.Search(5))

    // 刪除指定索引的數(shù)據(jù)項(xiàng)
    a.Remove(0)
    fmt.Println(a.Slice())

    // 并發(fā)安全,寫鎖操作
    a.LockFunc(func(array []int) {
        // 將末尾項(xiàng)改為100
        array[len(array) - 1] = 100
    })

    // 并發(fā)安全,讀鎖操作
    a.RLockFunc(func(array []int) {
        fmt.Println(array[len(array) - 1])
    })

    // 清空數(shù)組
    fmt.Println(a.Slice())
    a.Clear()
    fmt.Println(a.Slice())
}

執(zhí)行后,輸出結(jié)果為:

站在用戶的角度思考問題,與客戶深入溝通,找到東勝網(wǎng)站設(shè)計(jì)與東勝網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、國際域名空間、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋東勝地區(qū)。

10
[0 1 2 3 4 5 6 7 8 9]
6 true
[0 1 2 3 4 5 6 7 8 9 10 11]
[100 1 2 3 4 5 6 7 8 9 10 11]
5
[1 2 3 4 5 6 7 8 9 10 11]
100
[1 2 3 4 5 6 7 8 9 10 100]
[]

排序數(shù)組

排序數(shù)組的方法與普通數(shù)組類似,但是帶有自動(dòng)排序功能及唯一性過濾功能。

package main

import (
    "fmt"
    "github.com/gogf/gf/v2/container/garray"
)


func main () {
    // 自定義排序數(shù)組,降序排序(SortedIntArray管理的數(shù)據(jù)是升序)
    a := garray.NewSortedArray(func(v1, v2 interface{}) int {
        if v1.(int) < v2.(int) {
            return 1
        }
        if v1.(int) > v2.(int) {
            return -1
        }
        return 0
    })

    // 添加數(shù)據(jù)
    a.Add(2)
    a.Add(3)
    a.Add(1)
    fmt.Println(a.Slice())

    // 添加重復(fù)數(shù)據(jù)
    a.Add(3)
    fmt.Println(a.Slice())

    // 檢索數(shù)據(jù),返回最后對比的索引位置,檢索結(jié)果
    // 檢索結(jié)果:0: 匹配; <0:參數(shù)小于對比值; >0:參數(shù)大于對比值
    fmt.Println(a.Search(1))

    // 設(shè)置不可重復(fù)
    a.SetUnique(true)
    fmt.Println(a.Slice())
    a.Add(1)
    fmt.Println(a.Slice())
}

執(zhí)行后,輸出結(jié)果:

[3 2 1]
[3 3 2 1]
3 0
[3 2 1]
[3 2 1]

Iterate*數(shù)組遍歷

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array := garray.NewStrArrayFrom(g.SliceStr{"a", "b", "c"})
	// Iterator is alias of IteratorAsc, which iterates the array readonly in ascending order
	//  with given callback function .
	// If  returns true, then it continues iterating; or false to stop.
	array.Iterator(func(k int, v string) bool {
		fmt.Println(k, v)
		return true
	})
	// IteratorDesc iterates the array readonly in descending order with given callback function .
	// If  returns true, then it continues iterating; or false to stop.
	array.IteratorDesc(func(k int, v string) bool {
		fmt.Println(k, v)
		return true
	})

	// Output:
	// 0 a
	// 1 b
	// 2 c
	// 2 c
	// 1 b
	// 0 a
}

Pop*數(shù)組項(xiàng)出棧

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
)

func main() {
	array := garray.NewFrom([]interface{}{1, 2, 3, 4, 5, 6, 7, 8, 9})

	// Any Pop* functions pick, delete and return the item from array.

	fmt.Println(array.PopLeft())
	fmt.Println(array.PopLefts(2))
	fmt.Println(array.PopRight())
	fmt.Println(array.PopRights(2))

	// Output:
	// 1 true
	// [2 3]
	// 9 true
	// [7 8]
}

Rand/PopRand數(shù)組項(xiàng)隨機(jī)獲取/出棧

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

	// Randomly retrieve and return 2 items from the array.
	// It does not delete the items from array.
	fmt.Println(array.Rands(2))

	// Randomly pick and return one item from the array.
	// It deletes the picked up item from array.
	fmt.Println(array.PopRand())
}

Contains/ContainsI包含判斷

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
)

func main() {
	var array garray.StrArray
	array.Append("a")
	fmt.Println(array.Contains("a"))
	fmt.Println(array.Contains("A"))
	fmt.Println(array.ContainsI("A"))

	// Output:
	// true
	// false
	// true
}

FilterEmpty/FilterNil空值過濾

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array1 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
	array2 := garray.NewFrom(g.Slice{0, 1, 2, nil, "", g.Slice{}, "john"})
	fmt.Printf("%#v\n", array1.FilterNil().Slice())
	fmt.Printf("%#v\n", array2.FilterEmpty().Slice())

	// Output:
	// []interface {}{0, 1, 2, "", []interface {}{}, "john"}
	// []interface {}{1, 2, "john"}
}

Reverse數(shù)組翻轉(zhuǎn)

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

	// Reverse makes array with elements in reverse order.
	fmt.Println(array.Reverse().Slice())

	// Output:
	// [9 8 7 6 5 4 3 2 1]
}

Shuffle隨機(jī)排序

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

	// Shuffle randomly shuffles the array.
	fmt.Println(array.Shuffle().Slice())
}

Walk遍歷修改

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	var array garray.StrArray
	tables := g.SliceStr{"user", "user_detail"}
	prefix := "gf_"
	array.Append(tables...)
	// Add prefix for given table names.
	array.Walk(func(value string) string {
		return prefix + value
	})
	fmt.Println(array.Slice())

	// Output:
	// [gf_user gf_user_detail]
}

Join數(shù)組項(xiàng)串連

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array := garray.NewFrom(g.Slice{"a", "b", "c", "d"})
	fmt.Println(array.Join(","))

	// Output:
	// a,b,c,d
}

Chunk數(shù)組拆分

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array := garray.NewFrom(g.Slice{1, 2, 3, 4, 5, 6, 7, 8, 9})

	// Chunk splits an array into multiple arrays,
	// the size of each array is determined by .
	// The last chunk may contain less than size elements.
	fmt.Println(array.Chunk(2))

	// Output:
	// [[1 2] [3 4] [5 6] [7 8] [9]]}

Merge數(shù)組合并

package main

import (
	"fmt"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/frame/g"
)

func main() {
	array1 := garray.NewFrom(g.Slice{1, 2})
	array2 := garray.NewFrom(g.Slice{3, 4})
	slice1 := g.Slice{5, 6}
	slice2 := []int{7, 8}
	slice3 := []string{"9", "0"}
	fmt.Println(array1.Slice())
	array1.Merge(array1)
	array1.Merge(array2)
	array1.Merge(slice1)
	array1.Merge(slice2)
	array1.Merge(slice3)
	fmt.Println(array1.Slice())

	// Output:
	// [1 2]
	// [1 2 1 2 3 4 5 6 7 8 9 0]
}

JSON序列化/反序列

?garray?模塊下的所有容器類型均實(shí)現(xiàn)了標(biāo)準(zhǔn)庫?json?數(shù)據(jù)格式的序列化/反序列化接口。

Marshal 

package main

import (
    "encoding/json"
    "fmt"
    "github.com/gogf/gf/v2/container/garray"
)

func main() {
    type Student struct {
        Id     int
        Name   string
        Scores *garray.IntArray
    }
    s := Student{
        Id:     1,
        Name:   "john",
        Scores: garray.NewIntArrayFrom([]int{100, 99, 98}),
    }
    b, _ := json.Marshal(s)
    fmt.Println(string(b))
}

執(zhí)行后,輸出結(jié)果:

 {"Id":1,"Name":"john","Scores":[100,99,98]} 

Unmarshal

package main


import (
    "encoding/json"
    "fmt"
    "github.com/gogf/gf/v2/container/garray"
)


func main() {
    b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
    type Student struct {
        Id     int
        Name   string
        Scores *garray.IntArray
    }
    s := Student{}
    json.Unmarshal(b, &s)
    fmt.Println(s)
}

執(zhí)行后,輸出結(jié)果:

{1 john [100,99,98]}

當(dāng)前題目:創(chuàng)新互聯(lián)GoFrame教程:GoFramegarray-基本使用
新聞來源:http://www.dlmjj.cn/article/cdehpgh.html