github.com/gogf/gf/v2@v2.7.4/container/gmap/gmap_z_example_any_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/gogf/gf.
     6  
     7  package gmap_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/gogf/gf/v2/container/gmap"
    13  	"github.com/gogf/gf/v2/frame/g"
    14  	"github.com/gogf/gf/v2/internal/json"
    15  	"github.com/gogf/gf/v2/util/gconv"
    16  )
    17  
    18  func ExampleAnyAnyMap_Iterator() {
    19  	m := gmap.New()
    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 interface{}, v interface{}) bool {
    26  		totalKey += k.(int)
    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 ExampleAnyAnyMap_Clone() {
    41  	m := gmap.New()
    42  
    43  	m.Set("key1", "val1")
    44  	fmt.Println(m)
    45  
    46  	n := m.Clone()
    47  	fmt.Println(n)
    48  
    49  	// Output:
    50  	// {"key1":"val1"}
    51  	// {"key1":"val1"}
    52  }
    53  
    54  func ExampleAnyAnyMap_Map() {
    55  	// non concurrent-safety, a pointer to the underlying data
    56  	m1 := gmap.New()
    57  	m1.Set("key1", "val1")
    58  	fmt.Println("m1:", m1)
    59  
    60  	n1 := m1.Map()
    61  	fmt.Println("before n1:", n1)
    62  	m1.Set("key1", "val2")
    63  	fmt.Println("after n1:", n1)
    64  
    65  	// concurrent-safety, copy of underlying data
    66  	m2 := gmap.New(true)
    67  	m2.Set("key1", "val1")
    68  	fmt.Println("m2:", m2)
    69  
    70  	n2 := m2.Map()
    71  	fmt.Println("before n2:", n2)
    72  	m2.Set("key1", "val2")
    73  	fmt.Println("after n2:", n2)
    74  
    75  	// Output:
    76  	// m1: {"key1":"val1"}
    77  	// before n1: map[key1:val1]
    78  	// after n1: map[key1:val2]
    79  	// m2: {"key1":"val1"}
    80  	// before n2: map[key1:val1]
    81  	// after n2: map[key1:val1]
    82  }
    83  
    84  func ExampleAnyAnyMap_MapCopy() {
    85  	m := gmap.New()
    86  
    87  	m.Set("key1", "val1")
    88  	m.Set("key2", "val2")
    89  	fmt.Println(m)
    90  
    91  	n := m.MapCopy()
    92  	fmt.Println(n)
    93  
    94  	// Output:
    95  	// {"key1":"val1","key2":"val2"}
    96  	// map[key1:val1 key2:val2]
    97  }
    98  
    99  func ExampleAnyAnyMap_MapStrAny() {
   100  	m := gmap.New()
   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 ExampleAnyAnyMap_FilterEmpty() {
   112  	m := gmap.NewFrom(g.MapAnyAny{
   113  		"k1": "",
   114  		"k2": nil,
   115  		"k3": 0,
   116  		"k4": 1,
   117  	})
   118  	m.FilterEmpty()
   119  	fmt.Println(m.Map())
   120  
   121  	// Output:
   122  	// map[k4:1]
   123  }
   124  
   125  func ExampleAnyAnyMap_FilterNil() {
   126  	m := gmap.NewFrom(g.MapAnyAny{
   127  		"k1": "",
   128  		"k2": nil,
   129  		"k3": 0,
   130  		"k4": 1,
   131  	})
   132  	m.FilterNil()
   133  	fmt.Printf("%#v", m.Map())
   134  
   135  	// Output:
   136  	// map[interface {}]interface {}{"k1":"", "k3":0, "k4":1}
   137  }
   138  
   139  func ExampleAnyAnyMap_Set() {
   140  	m := gmap.New()
   141  
   142  	m.Set("key1", "val1")
   143  	fmt.Println(m)
   144  
   145  	// Output:
   146  	// {"key1":"val1"}
   147  }
   148  
   149  func ExampleAnyAnyMap_Sets() {
   150  	m := gmap.New()
   151  
   152  	addMap := make(map[interface{}]interface{})
   153  	addMap["key1"] = "val1"
   154  	addMap["key2"] = "val2"
   155  	addMap["key3"] = "val3"
   156  
   157  	m.Sets(addMap)
   158  	fmt.Println(m)
   159  
   160  	// Output:
   161  	// {"key1":"val1","key2":"val2","key3":"val3"}
   162  }
   163  
   164  func ExampleAnyAnyMap_Search() {
   165  	m := gmap.New()
   166  
   167  	m.Set("key1", "val1")
   168  
   169  	value, found := m.Search("key1")
   170  	if found {
   171  		fmt.Println("find key1 value:", value)
   172  	}
   173  
   174  	value, found = m.Search("key2")
   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 ExampleAnyAnyMap_Get() {
   185  	m := gmap.New()
   186  
   187  	m.Set("key1", "val1")
   188  
   189  	fmt.Println("key1 value:", m.Get("key1"))
   190  	fmt.Println("key2 value:", m.Get("key2"))
   191  
   192  	// Output:
   193  	// key1 value: val1
   194  	// key2 value: <nil>
   195  }
   196  
   197  func ExampleAnyAnyMap_Pop() {
   198  	var m gmap.Map
   199  	m.Sets(g.MapAnyAny{
   200  		"k1": "v1",
   201  		"k2": "v2",
   202  		"k3": "v3",
   203  		"k4": "v4",
   204  	})
   205  
   206  	fmt.Println(m.Pop())
   207  
   208  	// May Output:
   209  	// k1 v1
   210  }
   211  
   212  func ExampleAnyAnyMap_Pops() {
   213  	var m gmap.Map
   214  	m.Sets(g.MapAnyAny{
   215  		"k1": "v1",
   216  		"k2": "v2",
   217  		"k3": "v3",
   218  		"k4": "v4",
   219  	})
   220  	fmt.Println(m.Pops(-1))
   221  	fmt.Println("size:", m.Size())
   222  
   223  	m.Sets(g.MapAnyAny{
   224  		"k1": "v1",
   225  		"k2": "v2",
   226  		"k3": "v3",
   227  		"k4": "v4",
   228  	})
   229  	fmt.Println(m.Pops(2))
   230  	fmt.Println("size:", m.Size())
   231  
   232  	// May Output:
   233  	// map[k1:v1 k2:v2 k3:v3 k4:v4]
   234  	// size: 0
   235  	// map[k1:v1 k2:v2]
   236  	// size: 2
   237  }
   238  
   239  func ExampleAnyAnyMap_GetOrSet() {
   240  	m := gmap.New()
   241  	m.Set("key1", "val1")
   242  
   243  	fmt.Println(m.GetOrSet("key1", "NotExistValue"))
   244  	fmt.Println(m.GetOrSet("key2", "val2"))
   245  
   246  	// Output:
   247  	// val1
   248  	// val2
   249  }
   250  
   251  func ExampleAnyAnyMap_GetOrSetFunc() {
   252  	m := gmap.New()
   253  	m.Set("key1", "val1")
   254  
   255  	fmt.Println(m.GetOrSetFunc("key1", func() interface{} {
   256  		return "NotExistValue"
   257  	}))
   258  	fmt.Println(m.GetOrSetFunc("key2", func() interface{} {
   259  		return "NotExistValue"
   260  	}))
   261  
   262  	// Output:
   263  	// val1
   264  	// NotExistValue
   265  }
   266  
   267  func ExampleAnyAnyMap_GetOrSetFuncLock() {
   268  	m := gmap.New()
   269  	m.Set("key1", "val1")
   270  
   271  	fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} {
   272  		return "NotExistValue"
   273  	}))
   274  	fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} {
   275  		return "NotExistValue"
   276  	}))
   277  
   278  	// Output:
   279  	// val1
   280  	// NotExistValue
   281  }
   282  
   283  func ExampleAnyAnyMap_GetVar() {
   284  	m := gmap.New()
   285  	m.Set("key1", "val1")
   286  
   287  	fmt.Println(m.GetVar("key1"))
   288  	fmt.Println(m.GetVar("key2").IsNil())
   289  
   290  	// Output:
   291  	// val1
   292  	// true
   293  }
   294  
   295  func ExampleAnyAnyMap_GetVarOrSet() {
   296  	m := gmap.New()
   297  	m.Set("key1", "val1")
   298  
   299  	fmt.Println(m.GetVarOrSet("key1", "NotExistValue"))
   300  	fmt.Println(m.GetVarOrSet("key2", "val2"))
   301  
   302  	// Output:
   303  	// val1
   304  	// val2
   305  }
   306  
   307  func ExampleAnyAnyMap_GetVarOrSetFunc() {
   308  	m := gmap.New()
   309  	m.Set("key1", "val1")
   310  
   311  	fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} {
   312  		return "NotExistValue"
   313  	}))
   314  	fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} {
   315  		return "NotExistValue"
   316  	}))
   317  
   318  	// Output:
   319  	// val1
   320  	// NotExistValue
   321  }
   322  
   323  func ExampleAnyAnyMap_GetVarOrSetFuncLock() {
   324  	m := gmap.New()
   325  	m.Set("key1", "val1")
   326  
   327  	fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} {
   328  		return "NotExistValue"
   329  	}))
   330  	fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} {
   331  		return "NotExistValue"
   332  	}))
   333  
   334  	// Output:
   335  	// val1
   336  	// NotExistValue
   337  }
   338  
   339  func ExampleAnyAnyMap_SetIfNotExist() {
   340  	var m gmap.Map
   341  	fmt.Println(m.SetIfNotExist("k1", "v1"))
   342  	fmt.Println(m.SetIfNotExist("k1", "v2"))
   343  	fmt.Println(m.Map())
   344  
   345  	// Output:
   346  	// true
   347  	// false
   348  	// map[k1:v1]
   349  }
   350  
   351  func ExampleAnyAnyMap_SetIfNotExistFunc() {
   352  	var m gmap.Map
   353  	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
   354  		return "v1"
   355  	}))
   356  	fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} {
   357  		return "v2"
   358  	}))
   359  	fmt.Println(m.Map())
   360  
   361  	// Output:
   362  	// true
   363  	// false
   364  	// map[k1:v1]
   365  }
   366  
   367  func ExampleAnyAnyMap_SetIfNotExistFuncLock() {
   368  	var m gmap.Map
   369  	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
   370  		return "v1"
   371  	}))
   372  	fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} {
   373  		return "v2"
   374  	}))
   375  	fmt.Println(m.Map())
   376  
   377  	// Output:
   378  	// true
   379  	// false
   380  	// map[k1:v1]
   381  }
   382  
   383  func ExampleAnyAnyMap_Remove() {
   384  	var m gmap.Map
   385  	m.Set("k1", "v1")
   386  
   387  	fmt.Println(m.Remove("k1"))
   388  	fmt.Println(m.Remove("k2"))
   389  	fmt.Println(m.Size())
   390  
   391  	// Output:
   392  	// v1
   393  	// <nil>
   394  	// 0
   395  }
   396  
   397  func ExampleAnyAnyMap_Removes() {
   398  	var m gmap.Map
   399  	m.Sets(g.MapAnyAny{
   400  		"k1": "v1",
   401  		"k2": "v2",
   402  		"k3": "v3",
   403  		"k4": "v4",
   404  	})
   405  
   406  	removeList := make([]interface{}, 2)
   407  	removeList = append(removeList, "k1")
   408  	removeList = append(removeList, "k2")
   409  
   410  	m.Removes(removeList)
   411  
   412  	fmt.Println(m.Map())
   413  
   414  	// Output:
   415  	// map[k3:v3 k4:v4]
   416  }
   417  
   418  func ExampleAnyAnyMap_Keys() {
   419  	var m gmap.Map
   420  	m.Sets(g.MapAnyAny{
   421  		"k1": "v1",
   422  		"k2": "v2",
   423  		"k3": "v3",
   424  		"k4": "v4",
   425  	})
   426  	fmt.Println(m.Keys())
   427  
   428  	// May Output:
   429  	// [k1 k2 k3 k4]
   430  }
   431  
   432  func ExampleAnyAnyMap_Values() {
   433  	var m gmap.Map
   434  	m.Sets(g.MapAnyAny{
   435  		"k1": "v1",
   436  		"k2": "v2",
   437  		"k3": "v3",
   438  		"k4": "v4",
   439  	})
   440  	fmt.Println(m.Values())
   441  
   442  	// May Output:
   443  	// [v1 v2 v3 v4]
   444  }
   445  
   446  func ExampleAnyAnyMap_Contains() {
   447  	var m gmap.Map
   448  	m.Sets(g.MapAnyAny{
   449  		"k1": "v1",
   450  		"k2": "v2",
   451  		"k3": "v3",
   452  		"k4": "v4",
   453  	})
   454  
   455  	fmt.Println(m.Contains("k1"))
   456  	fmt.Println(m.Contains("k5"))
   457  
   458  	// Output:
   459  	// true
   460  	// false
   461  }
   462  
   463  func ExampleAnyAnyMap_Size() {
   464  	var m gmap.Map
   465  	m.Sets(g.MapAnyAny{
   466  		"k1": "v1",
   467  		"k2": "v2",
   468  		"k3": "v3",
   469  		"k4": "v4",
   470  	})
   471  
   472  	fmt.Println(m.Size())
   473  
   474  	// Output:
   475  	// 4
   476  }
   477  
   478  func ExampleAnyAnyMap_IsEmpty() {
   479  	var m gmap.Map
   480  	fmt.Println(m.IsEmpty())
   481  
   482  	m.Set("k1", "v1")
   483  	fmt.Println(m.IsEmpty())
   484  
   485  	// Output:
   486  	// true
   487  	// false
   488  }
   489  
   490  func ExampleAnyAnyMap_Clear() {
   491  	var m gmap.Map
   492  	m.Sets(g.MapAnyAny{
   493  		"k1": "v1",
   494  		"k2": "v2",
   495  		"k3": "v3",
   496  		"k4": "v4",
   497  	})
   498  
   499  	m.Clear()
   500  
   501  	fmt.Println(m.Map())
   502  
   503  	// Output:
   504  	// map[]
   505  }
   506  
   507  func ExampleAnyAnyMap_Replace() {
   508  	var m gmap.Map
   509  	m.Sets(g.MapAnyAny{
   510  		"k1": "v1",
   511  	})
   512  
   513  	var n gmap.Map
   514  	n.Sets(g.MapAnyAny{
   515  		"k2": "v2",
   516  	})
   517  
   518  	fmt.Println(m.Map())
   519  
   520  	m.Replace(n.Map())
   521  	fmt.Println(m.Map())
   522  
   523  	n.Set("k2", "v1")
   524  	fmt.Println(m.Map())
   525  
   526  	// Output:
   527  	// map[k1:v1]
   528  	// map[k2:v2]
   529  	// map[k2:v1]
   530  }
   531  
   532  func ExampleAnyAnyMap_LockFunc() {
   533  	var m gmap.Map
   534  	m.Sets(g.MapAnyAny{
   535  		"k1": 1,
   536  		"k2": 2,
   537  		"k3": 3,
   538  		"k4": 4,
   539  	})
   540  
   541  	m.LockFunc(func(m map[interface{}]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 ExampleAnyAnyMap_RLockFunc() {
   554  	var m gmap.Map
   555  	m.Sets(g.MapAnyAny{
   556  		"k1": 1,
   557  		"k2": 2,
   558  		"k3": 3,
   559  		"k4": 4,
   560  	})
   561  
   562  	m.RLockFunc(func(m map[interface{}]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 ExampleAnyAnyMap_Flip() {
   575  	var m gmap.Map
   576  	m.Sets(g.MapAnyAny{
   577  		"k1": "v1",
   578  	})
   579  	m.Flip()
   580  	fmt.Println(m.Map())
   581  
   582  	// Output:
   583  	// map[v1:k1]
   584  }
   585  
   586  func ExampleAnyAnyMap_Merge() {
   587  	var m1, m2 gmap.Map
   588  	m1.Set("key1", "val1")
   589  	m2.Set("key2", "val2")
   590  	m1.Merge(&m2)
   591  	fmt.Println(m1.Map())
   592  
   593  	// May Output:
   594  	// map[key1:val1 key2:val2]
   595  }
   596  
   597  func ExampleAnyAnyMap_String() {
   598  	var m gmap.Map
   599  	m.Sets(g.MapAnyAny{
   600  		"k1": "v1",
   601  	})
   602  
   603  	fmt.Println(m.String())
   604  
   605  	var m1 *gmap.Map = nil
   606  	fmt.Println(len(m1.String()))
   607  
   608  	// Output:
   609  	// {"k1":"v1"}
   610  	// 0
   611  }
   612  
   613  func ExampleAnyAnyMap_MarshalJSON() {
   614  	var m gmap.Map
   615  	m.Sets(g.MapAnyAny{
   616  		"k1": "v1",
   617  		"k2": "v2",
   618  		"k3": "v3",
   619  		"k4": "v4",
   620  	})
   621  
   622  	bytes, err := json.Marshal(&m)
   623  	if err == nil {
   624  		fmt.Println(gconv.String(bytes))
   625  	}
   626  
   627  	// Output:
   628  	// {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"}
   629  }
   630  
   631  func ExampleAnyAnyMap_UnmarshalJSON() {
   632  	var m gmap.Map
   633  	m.Sets(g.MapAnyAny{
   634  		"k1": "v1",
   635  		"k2": "v2",
   636  		"k3": "v3",
   637  		"k4": "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[k1:v1 k2:v2 k3:v3 k4:v4]
   649  }
   650  
   651  func ExampleAnyAnyMap_UnmarshalValue() {
   652  	type User struct {
   653  		Uid   int
   654  		Name  string
   655  		Pass1 string `gconv:"password1"`
   656  		Pass2 string `gconv:"password2"`
   657  	}
   658  
   659  	var (
   660  		m    gmap.AnyAnyMap
   661  		user = User{
   662  			Uid:   1,
   663  			Name:  "john",
   664  			Pass1: "123",
   665  			Pass2: "456",
   666  		}
   667  	)
   668  	if err := gconv.Scan(user, &m); err == nil {
   669  		fmt.Printf("%#v", m.Map())
   670  	}
   671  
   672  	// Output:
   673  	// map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}
   674  }