github.com/gogf/gf/v2@v2.7.4/container/gmap/gmap_z_example_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/util/gutil"
    14  )
    15  
    16  func ExampleNew() {
    17  	m := gmap.New()
    18  
    19  	// Add data.
    20  	m.Set("key1", "val1")
    21  
    22  	// Print size.
    23  	fmt.Println(m.Size())
    24  
    25  	addMap := make(map[interface{}]interface{})
    26  	addMap["key2"] = "val2"
    27  	addMap["key3"] = "val3"
    28  	addMap[1] = 1
    29  
    30  	fmt.Println(m.Values())
    31  
    32  	// Batch add data.
    33  	m.Sets(addMap)
    34  
    35  	// Gets the value of the corresponding key.
    36  	fmt.Println(m.Get("key3"))
    37  
    38  	// Get the value by key, or set it with given key-value if not exist.
    39  	fmt.Println(m.GetOrSet("key4", "val4"))
    40  
    41  	// Set key-value if the key does not exist, then return true; or else return false.
    42  	fmt.Println(m.SetIfNotExist("key3", "val3"))
    43  
    44  	// Remove key
    45  	m.Remove("key2")
    46  	fmt.Println(m.Keys())
    47  
    48  	// Batch remove keys.
    49  	m.Removes([]interface{}{"key1", 1})
    50  	fmt.Println(m.Keys())
    51  
    52  	// Contains checks whether a key exists.
    53  	fmt.Println(m.Contains("key3"))
    54  
    55  	// Flip exchanges key-value of the map, it will change key-value to value-key.
    56  	m.Flip()
    57  	fmt.Println(m.Map())
    58  
    59  	// Clear deletes all data of the map.
    60  	m.Clear()
    61  
    62  	fmt.Println(m.Size())
    63  
    64  	// May Output:
    65  	// 1
    66  	// [val1]
    67  	// val3
    68  	// val4
    69  	// false
    70  	// [key4 key1 key3 1]
    71  	// [key4 key3]
    72  	// true
    73  	// map[val3:key3 val4:key4]
    74  	// 0
    75  }
    76  
    77  func ExampleNewFrom() {
    78  	m := gmap.New()
    79  
    80  	m.Set("key1", "val1")
    81  	fmt.Println(m)
    82  
    83  	n := gmap.NewFrom(m.MapCopy(), true)
    84  	fmt.Println(n)
    85  
    86  	// Output:
    87  	// {"key1":"val1"}
    88  	// {"key1":"val1"}
    89  }
    90  
    91  func ExampleNewHashMap() {
    92  	m := gmap.NewHashMap()
    93  
    94  	m.Set("key1", "val1")
    95  	fmt.Println(m)
    96  
    97  	// Output:
    98  	// {"key1":"val1"}
    99  }
   100  
   101  func ExampleNewHashMapFrom() {
   102  	m := gmap.New()
   103  
   104  	m.Set("key1", "val1")
   105  	fmt.Println(m)
   106  
   107  	n := gmap.NewHashMapFrom(m.MapCopy(), true)
   108  	fmt.Println(n)
   109  
   110  	// Output:
   111  	// {"key1":"val1"}
   112  	// {"key1":"val1"}
   113  }
   114  
   115  func ExampleNewAnyAnyMap() {
   116  	m := gmap.NewAnyAnyMap()
   117  
   118  	m.Set("key1", "val1")
   119  	fmt.Println(m)
   120  
   121  	// Output:
   122  	// {"key1":"val1"}
   123  }
   124  
   125  func ExampleNewAnyAnyMapFrom() {
   126  	m := gmap.NewAnyAnyMap()
   127  
   128  	m.Set("key1", "val1")
   129  	fmt.Println(m)
   130  
   131  	n := gmap.NewAnyAnyMapFrom(m.MapCopy(), true)
   132  	fmt.Println(n)
   133  
   134  	// Output:
   135  	// {"key1":"val1"}
   136  	// {"key1":"val1"}
   137  }
   138  
   139  func ExampleNewIntAnyMap() {
   140  	m := gmap.NewIntAnyMap()
   141  
   142  	m.Set(1, "val1")
   143  	fmt.Println(m)
   144  
   145  	// Output:
   146  	// {"1":"val1"}
   147  }
   148  
   149  func ExampleNewIntAnyMapFrom() {
   150  	m := gmap.NewIntAnyMap()
   151  
   152  	m.Set(1, "val1")
   153  	fmt.Println(m)
   154  
   155  	n := gmap.NewIntAnyMapFrom(m.MapCopy(), true)
   156  	fmt.Println(n)
   157  
   158  	// Output:
   159  	// {"1":"val1"}
   160  	// {"1":"val1"}
   161  }
   162  
   163  func ExampleNewIntIntMap() {
   164  	m := gmap.NewIntIntMap()
   165  
   166  	m.Set(1, 1)
   167  	fmt.Println(m)
   168  
   169  	// Output:
   170  	// {"1":1}
   171  }
   172  
   173  func ExampleNewIntIntMapFrom() {
   174  	m := gmap.NewIntIntMap()
   175  
   176  	m.Set(1, 1)
   177  	fmt.Println(m)
   178  
   179  	n := gmap.NewIntIntMapFrom(m.MapCopy(), true)
   180  	fmt.Println(n)
   181  
   182  	// Output:
   183  	// {"1":1}
   184  	// {"1":1}
   185  }
   186  
   187  func ExampleNewStrAnyMap() {
   188  	m := gmap.NewStrAnyMap()
   189  
   190  	m.Set("key1", "var1")
   191  	fmt.Println(m)
   192  
   193  	// Output:
   194  	// {"key1":"var1"}
   195  }
   196  
   197  func ExampleNewStrAnyMapFrom() {
   198  	m := gmap.NewStrAnyMap()
   199  
   200  	m.Set("key1", "var1")
   201  	fmt.Println(m)
   202  
   203  	n := gmap.NewStrAnyMapFrom(m.MapCopy(), true)
   204  	fmt.Println(n)
   205  
   206  	// Output:
   207  	// {"key1":"var1"}
   208  	// {"key1":"var1"}
   209  }
   210  
   211  func ExampleNewStrIntMap() {
   212  	m := gmap.NewStrIntMap()
   213  
   214  	m.Set("key1", 1)
   215  	fmt.Println(m)
   216  
   217  	// Output:
   218  	// {"key1":1}
   219  }
   220  
   221  func ExampleNewStrIntMapFrom() {
   222  	m := gmap.NewStrIntMap()
   223  
   224  	m.Set("key1", 1)
   225  	fmt.Println(m)
   226  
   227  	n := gmap.NewStrIntMapFrom(m.MapCopy(), true)
   228  	fmt.Println(n)
   229  
   230  	// Output:
   231  	// {"key1":1}
   232  	// {"key1":1}
   233  }
   234  
   235  func ExampleNewStrStrMap() {
   236  	m := gmap.NewStrStrMap()
   237  
   238  	m.Set("key1", "var1")
   239  	fmt.Println(m)
   240  
   241  	// Output:
   242  	// {"key1":"var1"}
   243  }
   244  
   245  func ExampleNewStrStrMapFrom() {
   246  	m := gmap.NewStrStrMap()
   247  
   248  	m.Set("key1", "var1")
   249  	fmt.Println(m)
   250  
   251  	n := gmap.NewStrStrMapFrom(m.MapCopy(), true)
   252  	fmt.Println(n)
   253  
   254  	// Output:
   255  	// {"key1":"var1"}
   256  	// {"key1":"var1"}
   257  }
   258  
   259  func ExampleNewListMap() {
   260  	m := gmap.NewListMap()
   261  
   262  	m.Set("key1", "var1")
   263  	m.Set("key2", "var2")
   264  	fmt.Println(m)
   265  
   266  	// Output:
   267  	// {"key1":"var1","key2":"var2"}
   268  }
   269  
   270  func ExampleNewListMapFrom() {
   271  	m := gmap.NewListMap()
   272  
   273  	m.Set("key1", "var1")
   274  	m.Set("key2", "var2")
   275  	fmt.Println(m)
   276  
   277  	n := gmap.NewListMapFrom(m.Map(), true)
   278  	fmt.Println(n)
   279  
   280  	// May Output:
   281  	// {"key1":"var1","key2":"var2"}
   282  	// {"key1":"var1","key2":"var2"}
   283  }
   284  
   285  func ExampleNewTreeMap() {
   286  	m := gmap.NewTreeMap(gutil.ComparatorString)
   287  
   288  	m.Set("key2", "var2")
   289  	m.Set("key1", "var1")
   290  
   291  	fmt.Println(m.Map())
   292  
   293  	// May Output:
   294  	// map[key1:var1 key2:var2]
   295  }
   296  
   297  func ExampleNewTreeMapFrom() {
   298  	m := gmap.NewTreeMap(gutil.ComparatorString)
   299  
   300  	m.Set("key2", "var2")
   301  	m.Set("key1", "var1")
   302  
   303  	fmt.Println(m.Map())
   304  
   305  	n := gmap.NewListMapFrom(m.Map(), true)
   306  	fmt.Println(n.Map())
   307  
   308  	// May Output:
   309  	// map[key1:var1 key2:var2]
   310  	// map[key1:var1 key2:var2]
   311  }