improve package gvar; remove gvar.Create

This commit is contained in:
John Guo
2022-01-10 20:33:09 +08:00
parent d4cb1666e5
commit 280c3b4a86
4 changed files with 8 additions and 177 deletions

View File

@ -26,28 +26,15 @@ type Var struct {
// The optional parameter `safe` specifies whether Var is used in concurrent-safety,
// which is false in default.
func New(value interface{}, safe ...bool) *Var {
v := Var{}
if len(safe) > 0 && !safe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
} else {
v.value = value
return &Var{
value: gtype.NewInterface(value),
safe: true,
}
}
return &v
}
// Create creates and returns a new Var with given `value`.
// The optional parameter `safe` specifies whether Var is used in concurrent-safety,
// which is false in default.
func Create(value interface{}, safe ...bool) Var {
v := Var{}
if len(safe) > 0 && !safe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
} else {
v.value = value
return &Var{
value: value,
}
return v
}
// Clone does a shallow copy of current Var and returns a pointer to this Var.

View File

@ -1,145 +0,0 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// go test *.go -bench=".*" -benchmem
package gvar
import "testing"
var varObj = Create(nil)
func Benchmark_Obj_Set(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Set(i)
}
}
func Benchmark_Obj_Val(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Val()
}
}
func Benchmark_Obj_IsNil(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.IsNil()
}
}
func Benchmark_Obj_Bytes(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Bytes()
}
}
func Benchmark_Obj_String(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.String()
}
}
func Benchmark_Obj_Bool(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Bool()
}
}
func Benchmark_Obj_Int(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int()
}
}
func Benchmark_Obj_Int8(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int8()
}
}
func Benchmark_Obj_Int16(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int16()
}
}
func Benchmark_Obj_Int32(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int32()
}
}
func Benchmark_Obj_Int64(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Int64()
}
}
func Benchmark_Obj_Uint(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint()
}
}
func Benchmark_Obj_Uint8(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint8()
}
}
func Benchmark_Obj_Uint16(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint16()
}
}
func Benchmark_Obj_Uint32(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint32()
}
}
func Benchmark_Obj_Uint64(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Uint64()
}
}
func Benchmark_Obj_Float32(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Float32()
}
}
func Benchmark_Obj_Float64(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Float64()
}
}
func Benchmark_Obj_Ints(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Ints()
}
}
func Benchmark_Obj_Strings(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Strings()
}
}
func Benchmark_Obj_Floats(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Floats()
}
}
func Benchmark_Obj_Interfaces(b *testing.B) {
for i := 0; i < b.N; i++ {
varObj.Interfaces()
}
}

View File

@ -8,6 +8,7 @@ package gvar_test
import (
"fmt"
"github.com/gogf/gf/v2/container/gvar"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/internal/json"
@ -23,15 +24,6 @@ func ExampleVarNew() {
// "400"
}
// Create
func ExampleVarCreate() {
var v = gvar.Create(100.0)
g.Dump(&v)
// Output:
// "100"
}
// Clone
func ExampleVar_Clone() {
tmp := "fisrt hello"

View File

@ -28,10 +28,7 @@ func Test_Set(t *testing.T) {
v.Set(123.456)
t.Assert(v.Val(), 123.456)
})
gtest.C(t, func(t *gtest.T) {
v := gvar.Create(123.456)
t.Assert(v.Val(), 123.456)
})
gtest.C(t, func(t *gtest.T) {
objOne := gvar.New("old", true)
objOneOld, _ := objOne.Set("new").(string)