Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

gtype

并发安全基本类型。

使用场景

gtype使用得非常频繁,任何需要并发安全的场景下都适用。

...

https://godoc.org/github.com/gogf/gf/container/gtype

性能测试

基准测试结果如下:

john@john-B85M:~/Workspace/Go/GOPATH/src/github.com/gogf/gf/container/gtype$ go test -bench=".*"  -benchmem
goos: linux
goarch: amd64
pkg: github.com/gogf/gf/container/gtype
BenchmarkInt_Set-4            300000000           5.87 ns/op        0 B/op        0 allocs/op
BenchmarkInt_Val-4            2000000000          0.46 ns/op        0 B/op        0 allocs/op
BenchmarkInt_Add-4            300000000           5.86 ns/op        0 B/op        0 allocs/op
BenchmarkInt32_Set-4          300000000           5.87 ns/op        0 B/op        0 allocs/op
BenchmarkInt32_Val-4          2000000000          0.47 ns/op        0 B/op        0 allocs/op
BenchmarkInt32_Add-4          300000000           5.85 ns/op        0 B/op        0 allocs/op
BenchmarkInt64_Set-4          300000000           5.88 ns/op        0 B/op        0 allocs/op
BenchmarkInt64_Val-4          2000000000          0.46 ns/op        0 B/op        0 allocs/op
BenchmarkInt64_Add-4          300000000           5.88 ns/op        0 B/op        0 allocs/op
BenchmarkUint_Set-4           300000000           5.88 ns/op        0 B/op        0 allocs/op
BenchmarkUint_Val-4           2000000000          0.46 ns/op        0 B/op        0 allocs/op
BenchmarkUint_Add-4           300000000           5.87 ns/op        0 B/op        0 allocs/op
BenchmarkUint32_Set-4         300000000           5.86 ns/op        0 B/op        0 allocs/op
BenchmarkUint32_Val-4         2000000000          0.50 ns/op        0 B/op        0 allocs/op
BenchmarkUint32_Add-4         200000000           5.86 ns/op        0 B/op        0 allocs/op
BenchmarkUint64_Set-4         300000000           5.86 ns/op        0 B/op        0 allocs/op
BenchmarkUint64_Val-4         2000000000          0.47 ns/op        0 B/op        0 allocs/op
BenchmarkUint64_Add-4         300000000           5.85 ns/op        0 B/op        0 allocs/op
BenchmarkBool_Set-4           300000000           5.85 ns/op        0 B/op        0 allocs/op
BenchmarkBool_Val-4           2000000000          0.46 ns/op        0 B/op        0 allocs/op
BenchmarkString_Set-4         20000000            90.1 ns/op       23 B/op        1 allocs/op
BenchmarkString_Val-4         2000000000          1.58 ns/op        0 B/op        0 allocs/op
BenchmarkBytes_Set-4          20000000            76.2 ns/op       35 B/op        2 allocs/op
BenchmarkBytes_Val-4          2000000000          1.58 ns/op        0 B/op        0 allocs/op
BenchmarkInterface_Set-4      50000000            30.7 ns/op        8 B/op        0 allocs/op
BenchmarkInterface_Val-4      2000000000          0.74 ns/op        0 B/op        0 allocs/op
BenchmarkAtomicValue_Store-4  50000000            27.3 ns/op        8 B/op        0 allocs/op
BenchmarkAtomicValue_Load-4   2000000000          0.73 ns/op        0 B/op        0 allocs/op
PASS
ok   github.com/gogf/gf/container/gtype 49.454s

使用示例

gtype并发安全基本类型的使用非常简单,往往就类似以下几个方法(以gtype.Int类型举例):

func NewInt(value ...int) *Int
func (v *Int) Add(delta int) (new int)
func (v *Int) Cas(old, new int) bool
func (v *Int) Clone() *Int
func (v *Int) Set(value int) (old int)
func (v *Int) String() string
func (v *Int) Val() int

示例1,基本使用

package main

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

func main() {
    // 创建一个Int型的并发安全基本类型对象
    i := gtype.NewInt()

    // 设置值
    fmt.Println(i.Set(10))

    // 获取值
    fmt.Println(i.Val())

    // 数值-1,并返回修改之后的数值
    fmt.Println(i.Add(-1))
}

执行后,输出结果为:

0
10
9

示例2,JSON序列化/反序列

gtype模块下的所有容器类型均实现了标准库json数据格式的序列化/反序列化接口。 1. Marshal “`go package main

...