github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_example_int_any_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with gm file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/wangyougui/gf/v2/container/gmap"
    13  	"github.com/wangyougui/gf/v2/frame/g"
    14  	"github.com/wangyougui/gf/v2/internal/json"
    15  	"github.com/wangyougui/gf/v2/util/gconv"
    16  )
    17  
    18  func ExampleIntAnyMap_Iterator() {
    19  	m := gmap.NewIntAnyMap()
    20  	for i := 0; i < 10; i++ {
    21  		m.Set(i, i*2)
    22  	}
    23  
    24  	var totalKey, totalValue int
    25  	m.Iterator(func(k int, v interface{}) bool {
    26  		totalKey += k
    27  		totalValue += v.(int)
    28  
    29  		return totalKey < 10
    30  	})
    31  
    32  	fmt.Println("totalKey:", totalKey)
    33  	fmt.Println("totalValue:", totalValue)
    34  
    35  	// May Output:
    36  	// totalKey: 11
    37  	// totalValue: 22
    38  }
    39  
    40  func ExampleIntAnyMap_Clone() {
    41  	m := gmap.NewIntAnyMap()
    42  
    43  	m.Set(1, "val1")
    44  	fmt.Println(m)
    45  
    46  	n := m.Clone()
    47  	fmt.Println(n)
    48  
    49  	// Output:
    50  	// {"1":"val1"}
    51  	// {"1":"val1"}
    52  }
    53  
    54  func ExampleIntAnyMap_Map() {
    55  	// non concurrent-safety, a pointer to the underlying data
    56  	m1 := gmap.NewIntAnyMap()
    57  	m1.Set(1, "val1")
    58  	fmt.Println("m1:", m1)
    59  
    60  	n1 := m1.Map()
    61  	fmt.Println("before n1:", n1)
    62  	m1.Set(1, "val2")
    63  	fmt.Println("after n1:", n1)
    64  
    65  	// concurrent-safety, copy of underlying data
    66  	m2 := gmap.New(true)
    67  	m2.Set(1, "val1")
    68  	fmt.Println("m2:", m2)
    69  
    70  	n2 := m2.Map()
    71  	fmt.Println("before n2:", n2)
    72  	m2.Set(1, "val2")
    73  	fmt.Println("after n2:", n2)
    74  
    75  	// Output:
    76  	// m1: {"1":"val1"}
    77  	// before n1: map[1:val1]
    78  	// after n1: map[1:val2]
    79  	// m2: {"1":"val1"}
    80  	// before n2: map[1:val1]
    81  	// after n2: map[1:val1]
    82  }
    83  
    84  func ExampleIntAnyMap_MapCopy() {
    85  	m := gmap.NewIntAnyMap()
    86  
    87  	m.Set(1, "val1")
    88  	m.Set(2, "val2")
    89  	fmt.Println(m)
    90  
    91  	n := m.MapCopy()
    92  	fmt.Println(n)
    93  
    94  	// Output:
    95  	// {"1":"val1","2":"val2"}
    96  	// map[1:val1 2:val2]
    97  }
    98  
    99  func ExampleIntAnyMap_MapStrAny() {
   100  	m := gmap.NewIntAnyMap()
   101  	m.Set(1001, "val1")
   102  	m.Set(1002, "val2")
   103  
   104  	n := m.MapStrAny()
   105  	fmt.Printf("%#v", n)
   106  
   107  	// Output:
   108  	// map[string]interface {}{"1001":"val1", "1002":"val2"}
   109  }
   110  
   111  func ExampleIntAnyMap_FilterEmpty() {
   112  	m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   113  		1: "",
   114  		2: nil,
   115  		3: 0,
   116  		4: 1,
   117  	})
   118  	m.FilterEmpty()
   119  	fmt.Println(m.Map())
   120  
   121  	// Output:
   122  	// map[4:1]
   123  }
   124  
   125  func ExampleIntAnyMap_FilterNil() {
   126  	m := gmap.NewIntAnyMapFrom(g.MapIntAny{
   127  		1: "",
   128  		2: nil,
   129  		3: 0,
   130  		4: 1,
   131  	})
   132  	m.FilterNil()
   133  	fmt.Printf("%#v", m.Map())
   134  
   135  	// Output:
   136  	// map[int]interface {}{1:"", 3:0, 4:1}
   137  }
   138  
   139  func ExampleIntAnyMap_Set() {
   140  	m := gmap.NewIntAnyMap()
   141  
   142  	m.Set(1, "val1")
   143  	fmt.Println(m)
   144  
   145  	// Output:
   146  	// {"1":"val1"}
   147  }
   148  
   149  func ExampleIntAnyMap_Sets() {
   150  	m := gmap.NewIntAnyMap()
   151  
   152  	addMap := make(map[int]interface{})
   153  	addMap[1] = "val1"
   154  	addMap[2] = "val2"
   155  	addMap[3] = "val3"
   156  
   157  	m.Sets(addMap)
   158  	fmt.Println(m)
   159  
   160  	// Output:
   161  	// {"1":"val1","2":"val2","3":"val3"}
   162  }
   163  
   164  func ExampleIntAnyMap_Search() {
   165  	m := gmap.NewIntAnyMap()
   166  
   167  	m.Set(1, "val1")
   168  
   169  	value, found := m.Search(1)
   170  	if found {
   171  		fmt.Println("find key1 value:", value)
   172  	}
   173  
   174  	value, found = m.Search(2)
   175  	if !found {
   176  		fmt.Println("key2 not find")
   177  	}
   178  
   179  	// Output:
   180  	// find key1 value: val1
   181  	// key2 not find
   182  }
   183  
   184  func ExampleIntAnyMap_Get() {
   185  	m := gmap.NewIntAnyMap()
   186  
   187  	m.Set(1, "val1")
   188  
   189  	fmt.Println("key1 value:", m.Get(1))
   190  	fmt.Println("key2 value:", m.Get(2))
   191  
   192  	// Output:
   193  	// key1 value: val1
   194  	// key2 value: <nil>
   195  }
   196  
   197  func ExampleIntAnyMap_Pop() {
   198  	var m gmap.IntAnyMap
   199  	m.Sets(g.MapIntAny{
   200  		1: "v1",
   201  		2: "v2",
   202  		3: "v3",
   203  		4: "v4",
   204  	})
   205  
   206  	fmt.Println(m.Pop())
   207  
   208  	// May Output:
   209  	// 1 v1
   210  }
   211  
   212  func ExampleIntAnyMap_Pops() {
   213  	var m gmap.IntAnyMap
   214  	m.Sets(g.MapIntAny{
   215  		1: "v1",
   216  		2: "v2",
   217  		3: "v3",
   218  		4: "v4",
   219  	})
   220  	fmt.Println(m.Pops(-1))
   221  	fmt.Println("size:", m.Size())
   222  
   223  	m.Sets(g.MapIntAny{
   224  		1: "v1",
   225  		2: "v2",
   226  		3: "v3",
   227  		4: "v4",
   228  	})
   229  	fmt.Println(m.Pops(2))
   230  	fmt.Println("size:", m.Size())
   231  
   232  	// May Output:
   233  	// map[1:v1 2:v2 3:v3 4:v4]
   234  	// size: 0
   235  	// map[1:v1 2:v2]
   236  	// size: 2
   237  }
   238  
   239  func ExampleIntAnyMap_GetOrSet() {
   240  	m := gmap.NewIntAnyMap()
   241  	m.Set(1, "val1")
   242  
   243  	fmt.Println(m.GetOrSet(1, "NotExistValue"))
   244  	fmt.Println(m.GetOrSet(2, "val2"))
   245  
   246  	// Output:
   247  	// val1
   248  	// val2
   249  }
   250  
   251  func ExampleIntAnyMap_GetOrSetFunc() {
   252  	m := gmap.NewIntAnyMap()
   253  	m.Set(1, "val1")
   254  
   255  	fmt.Println(m.GetOrSetFunc(1, func() interface{} {
   256  		return "NotExistValue"
   257  	}))
   258  	fmt.Println(m.GetOrSetFunc(2, func() interface{} {
   259  		return "NotExistValue"
   260  	}))
   261  
   262  	// Output:
   263  	// val1
   264  	// NotExistValue
   265  }
   266  
   267  func ExampleIntAnyMap_GetOrSetFuncLock() {
   268  	m := gmap.NewIntAnyMap()
   269  	m.Set(1, "val1")
   270  
   271  	fmt.Println(m.GetOrSetFuncLock(1, func() interface{} {
   272  		return "NotExistValue"
   273  	}))
   274  	fmt.Println(m.GetOrSetFuncLock(2, func() interface{} {
   275  		return "NotExistValue"
   276  	}))
   277  
   278  	// Output:
   279  	// val1
   280  	// NotExistValue
   281  }
   282  
   283  func ExampleIntAnyMap_GetVar() {
   284  	m := gmap.NewIntAnyMap()
   285  	m.Set(1, "val1")
   286  
   287  	fmt.Println(m.GetVar(1))
   288  	fmt.Println(m.GetVar(2).IsNil())
   289  
   290  	// Output:
   291  	// val1
   292  	// true
   293  }
   294  
   295  func ExampleIntAnyMap_GetVarOrSet() {
   296  	m := gmap.NewIntAnyMap()
   297  	m.Set(1, "val1")
   298  
   299  	fmt.Println(m.GetVarOrSet(1, "NotExistValue"))
   300  	fmt.Println(m.GetVarOrSet(2, "val2"))
   301  
   302  	// Output:
   303  	// val1
   304  	// val2
   305  }
   306  
   307  func ExampleIntAnyMap_GetVarOrSetFunc() {
   308  	m := gmap.NewIntAnyMap()
   309  	m.Set(1, "val1")
   310  
   311  	fmt.Println(m.GetVarOrSetFunc(1, func() interface{} {
   312  		return "NotExistValue"
   313  	}))
   314  	fmt.Println(m.GetVarOrSetFunc(2, func() interface{} {
   315  		return "NotExistValue"
   316  	}))
   317  
   318  	// Output:
   319  	// val1
   320  	// NotExistValue
   321  }
   322  
   323  func ExampleIntAnyMap_GetVarOrSetFuncLock() {
   324  	m := gmap.NewIntAnyMap()
   325  	m.Set(1, "val1")
   326  
   327  	fmt.Println(m.GetVarOrSetFuncLock(1, func() interface{} {
   328  		return "NotExistValue"
   329  	}))
   330  	fmt.Println(m.GetVarOrSetFuncLock(2, func() interface{} {
   331  		return "NotExistValue"
   332  	}))
   333  
   334  	// Output:
   335  	// val1
   336  	// NotExistValue
   337  }
   338  
   339  func ExampleIntAnyMap_SetIfNotExist() {
   340  	var m gmap.IntAnyMap
   341  	fmt.Println(m.SetIfNotExist(1, "v1"))
   342  	fmt.Println(m.SetIfNotExist(1, "v2"))
   343  	fmt.Println(m.Map())
   344  
   345  	// Output:
   346  	// true
   347  	// false
   348  	// map[1:v1]
   349  }
   350  
   351  func ExampleIntAnyMap_SetIfNotExistFunc() {
   352  	var m gmap.IntAnyMap
   353  	fmt.Println(m.SetIfNotExistFunc(1, func() interface{} {
   354  		return "v1"
   355  	}))
   356  	fmt.Println(m.SetIfNotExistFunc(1, func() interface{} {
   357  		return "v2"
   358  	}))
   359  	fmt.Println(m.Map())
   360  
   361  	// Output:
   362  	// true
   363  	// false
   364  	// map[1:v1]
   365  }
   366  
   367  func ExampleIntAnyMap_SetIfNotExistFuncLock() {
   368  	var m gmap.IntAnyMap
   369  	fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} {
   370  		return "v1"
   371  	}))
   372  	fmt.Println(m.SetIfNotExistFuncLock(1, func() interface{} {
   373  		return "v2"
   374  	}))
   375  	fmt.Println(m.Map())
   376  
   377  	// Output:
   378  	// true
   379  	// false
   380  	// map[1:v1]
   381  }
   382  
   383  func ExampleIntAnyMap_Remove() {
   384  	var m gmap.IntAnyMap
   385  	m.Set(1, "v1")
   386  
   387  	fmt.Println(m.Remove(1))
   388  	fmt.Println(m.Remove(2))
   389  	fmt.Println(m.Size())
   390  
   391  	// Output:
   392  	// v1
   393  	// <nil>
   394  	// 0
   395  }
   396  
   397  func ExampleIntAnyMap_Removes() {
   398  	var m gmap.IntAnyMap
   399  	m.Sets(g.MapIntAny{
   400  		1: "v1",
   401  		2: "v2",
   402  		3: "v3",
   403  		4: "v4",
   404  	})
   405  
   406  	removeList := make([]int, 2)
   407  	removeList = append(removeList, 1)
   408  	removeList = append(removeList, 2)
   409  
   410  	m.Removes(removeList)
   411  
   412  	fmt.Println(m.Map())
   413  
   414  	// Output:
   415  	// map[3:v3 4:v4]
   416  }
   417  
   418  func ExampleIntAnyMap_Keys() {
   419  	var m gmap.IntAnyMap
   420  	m.Sets(g.MapIntAny{
   421  		1: "v1",
   422  		2: "v2",
   423  		3: "v3",
   424  		4: "v4",
   425  	})
   426  	fmt.Println(m.Keys())
   427  
   428  	// May Output:
   429  	// [1 2 3 4]
   430  }
   431  
   432  func ExampleIntAnyMap_Values() {
   433  	var m gmap.IntAnyMap
   434  	m.Sets(g.MapIntAny{
   435  		1: "v1",
   436  		2: "v2",
   437  		3: "v3",
   438  		4: "v4",
   439  	})
   440  	fmt.Println(m.Values())
   441  
   442  	// May Output:
   443  	// [v1 v2 v3 v4]
   444  }
   445  
   446  func ExampleIntAnyMap_Contains() {
   447  	var m gmap.IntAnyMap
   448  	m.Sets(g.MapIntAny{
   449  		1: "v1",
   450  		2: "v2",
   451  		3: "v3",
   452  		4: "v4",
   453  	})
   454  
   455  	fmt.Println(m.Contains(1))
   456  	fmt.Println(m.Contains(5))
   457  
   458  	// Output:
   459  	// true
   460  	// false
   461  }
   462  
   463  func ExampleIntAnyMap_Size() {
   464  	var m gmap.IntAnyMap
   465  	m.Sets(g.MapIntAny{
   466  		1: "v1",
   467  		2: "v2",
   468  		3: "v3",
   469  		4: "v4",
   470  	})
   471  
   472  	fmt.Println(m.Size())
   473  
   474  	// Output:
   475  	// 4
   476  }
   477  
   478  func ExampleIntAnyMap_IsEmpty() {
   479  	var m gmap.IntAnyMap
   480  	fmt.Println(m.IsEmpty())
   481  
   482  	m.Set(1, "v1")
   483  	fmt.Println(m.IsEmpty())
   484  
   485  	// Output:
   486  	// true
   487  	// false
   488  }
   489  
   490  func ExampleIntAnyMap_Clear() {
   491  	var m gmap.IntAnyMap
   492  	m.Sets(g.MapIntAny{
   493  		1: "v1",
   494  		2: "v2",
   495  		3: "v3",
   496  		4: "v4",
   497  	})
   498  
   499  	m.Clear()
   500  
   501  	fmt.Println(m.Map())
   502  
   503  	// Output:
   504  	// map[]
   505  }
   506  
   507  func ExampleIntAnyMap_Replace() {
   508  	var m gmap.IntAnyMap
   509  	m.Sets(g.MapIntAny{
   510  		1: "v1",
   511  	})
   512  
   513  	var n gmap.IntAnyMap
   514  	n.Sets(g.MapIntAny{
   515  		2: "v2",
   516  	})
   517  
   518  	fmt.Println(m.Map())
   519  
   520  	m.Replace(n.Map())
   521  	fmt.Println(m.Map())
   522  
   523  	n.Set(2, "v1")
   524  	fmt.Println(m.Map())
   525  
   526  	// Output:
   527  	// map[1:v1]
   528  	// map[2:v2]
   529  	// map[2:v1]
   530  }
   531  
   532  func ExampleIntAnyMap_LockFunc() {
   533  	var m gmap.IntAnyMap
   534  	m.Sets(g.MapIntAny{
   535  		1: 1,
   536  		2: 2,
   537  		3: 3,
   538  		4: 4,
   539  	})
   540  
   541  	m.LockFunc(func(m map[int]interface{}) {
   542  		totalValue := 0
   543  		for _, v := range m {
   544  			totalValue += v.(int)
   545  		}
   546  		fmt.Println("totalValue:", totalValue)
   547  	})
   548  
   549  	// Output:
   550  	// totalValue: 10
   551  }
   552  
   553  func ExampleIntAnyMap_RLockFunc() {
   554  	var m gmap.IntAnyMap
   555  	m.Sets(g.MapIntAny{
   556  		1: 1,
   557  		2: 2,
   558  		3: 3,
   559  		4: 4,
   560  	})
   561  
   562  	m.RLockFunc(func(m map[int]interface{}) {
   563  		totalValue := 0
   564  		for _, v := range m {
   565  			totalValue += v.(int)
   566  		}
   567  		fmt.Println("totalValue:", totalValue)
   568  	})
   569  
   570  	// Output:
   571  	// totalValue: 10
   572  }
   573  
   574  func ExampleIntAnyMap_Flip() {
   575  	var m gmap.IntAnyMap
   576  	m.Sets(g.MapIntAny{
   577  		1: 10,
   578  	})
   579  	m.Flip()
   580  	fmt.Println(m.Map())
   581  
   582  	// Output:
   583  	// map[10:1]
   584  }
   585  
   586  func ExampleIntAnyMap_Merge() {
   587  	var m1, m2 gmap.Map
   588  	m1.Set(1, "val1")
   589  	m2.Set(2, "val2")
   590  	m1.Merge(&m2)
   591  	fmt.Println(m1.Map())
   592  
   593  	// May Output:
   594  	// map[key1:val1 key2:val2]
   595  }
   596  
   597  func ExampleIntAnyMap_String() {
   598  	var m gmap.IntAnyMap
   599  	m.Sets(g.MapIntAny{
   600  		1: "v1",
   601  	})
   602  
   603  	fmt.Println(m.String())
   604  
   605  	var m1 *gmap.IntAnyMap = nil
   606  	fmt.Println(len(m1.String()))
   607  
   608  	// Output:
   609  	// {"1":"v1"}
   610  	// 0
   611  }
   612  
   613  func ExampleIntAnyMap_MarshalJSON() {
   614  	var m gmap.IntAnyMap
   615  	m.Sets(g.MapIntAny{
   616  		1: "v1",
   617  		2: "v2",
   618  		3: "v3",
   619  		4: "v4",
   620  	})
   621  
   622  	bytes, err := json.Marshal(&m)
   623  	if err == nil {
   624  		fmt.Println(gconv.String(bytes))
   625  	}
   626  
   627  	// Output:
   628  	// {"1":"v1","2":"v2","3":"v3","4":"v4"}
   629  }
   630  
   631  func ExampleIntAnyMap_UnmarshalJSON() {
   632  	var m gmap.IntAnyMap
   633  	m.Sets(g.MapIntAny{
   634  		1: "v1",
   635  		2: "v2",
   636  		3: "v3",
   637  		4: "v4",
   638  	})
   639  
   640  	var n gmap.Map
   641  
   642  	err := json.Unmarshal(gconv.Bytes(m.String()), &n)
   643  	if err == nil {
   644  		fmt.Println(n.Map())
   645  	}
   646  
   647  	// Output:
   648  	// map[1:v1 2:v2 3:v3 4:v4]
   649  }
   650  
   651  func ExampleIntAnyMap_UnmarshalValue() {
   652  	var m gmap.IntAnyMap
   653  
   654  	goWeb := map[int]interface{}{
   655  		1: "goframe",
   656  		2: "gin",
   657  		3: "echo",
   658  	}
   659  
   660  	if err := gconv.Scan(goWeb, &m); err == nil {
   661  		fmt.Printf("%#v", m.Map())
   662  	}
   663  
   664  	// Output:
   665  	// map[int]interface {}{1:"goframe", 2:"gin", 3:"echo"}
   666  }