github.com/wangyougui/gf/v2@v2.6.5/container/gmap/gmap_z_example_list_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 ExampleListMap_Iterator() { 19 m := gmap.NewListMap() 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 // Output: 36 // totalKey: 10 37 // totalValue: 20 38 } 39 40 func ExampleListMap_IteratorAsc() { 41 m := gmap.NewListMap() 42 for i := 0; i < 10; i++ { 43 m.Set(i, i*2) 44 } 45 46 var totalKey, totalValue int 47 m.IteratorAsc(func(k interface{}, v interface{}) bool { 48 totalKey += k.(int) 49 totalValue += v.(int) 50 51 return totalKey < 10 52 }) 53 54 fmt.Println("totalKey:", totalKey) 55 fmt.Println("totalValue:", totalValue) 56 57 // Output: 58 // totalKey: 10 59 // totalValue: 20 60 } 61 62 func ExampleListMap_IteratorDesc() { 63 m := gmap.NewListMap() 64 for i := 0; i < 10; i++ { 65 m.Set(i, i*2) 66 } 67 68 var totalKey, totalValue int 69 m.IteratorDesc(func(k interface{}, v interface{}) bool { 70 totalKey += k.(int) 71 totalValue += v.(int) 72 73 return totalKey < 10 74 }) 75 76 fmt.Println("totalKey:", totalKey) 77 fmt.Println("totalValue:", totalValue) 78 79 // Output: 80 // totalKey: 17 81 // totalValue: 34 82 } 83 84 func ExampleListMap_Clone() { 85 m := gmap.NewListMap() 86 87 m.Set("key1", "val1") 88 fmt.Println(m) 89 90 n := m.Clone() 91 fmt.Println(n) 92 93 // Output: 94 // {"key1":"val1"} 95 // {"key1":"val1"} 96 } 97 98 func ExampleListMap_Clear() { 99 var m gmap.ListMap 100 m.Sets(g.MapAnyAny{ 101 "k1": "v1", 102 "k2": "v2", 103 "k3": "v3", 104 "k4": "v4", 105 }) 106 107 m.Clear() 108 109 fmt.Println(m.Map()) 110 111 // Output: 112 // map[] 113 } 114 115 func ExampleListMap_Replace() { 116 var m gmap.ListMap 117 m.Sets(g.MapAnyAny{ 118 "k1": "v1", 119 }) 120 121 var n gmap.ListMap 122 n.Sets(g.MapAnyAny{ 123 "k2": "v2", 124 }) 125 126 fmt.Println(m.Map()) 127 128 m.Replace(n.Map()) 129 fmt.Println(m.Map()) 130 131 // Output: 132 // map[k1:v1] 133 // map[k2:v2] 134 } 135 136 func ExampleListMap_Map() { 137 m1 := gmap.NewListMap() 138 m1.Set("key1", "val1") 139 fmt.Println("m1:", m1) 140 141 n1 := m1.Map() 142 fmt.Println("before n1:", n1) 143 m1.Set("key1", "val2") 144 fmt.Println("after n1:", n1) 145 146 // Output: 147 // m1: {"key1":"val1"} 148 // before n1: map[key1:val1] 149 // after n1: map[key1:val1] 150 } 151 152 func ExampleListMap_MapStrAny() { 153 m := gmap.NewListMap() 154 m.Set("key1", "val1") 155 m.Set("key2", "val2") 156 157 n := m.MapStrAny() 158 fmt.Printf("%#v", n) 159 160 // Output: 161 // map[string]interface {}{"key1":"val1", "key2":"val2"} 162 } 163 164 func ExampleListMap_FilterEmpty() { 165 m := gmap.NewListMapFrom(g.MapAnyAny{ 166 "k1": "", 167 "k2": nil, 168 "k3": 0, 169 "k4": 1, 170 }) 171 m.FilterEmpty() 172 fmt.Println(m.Map()) 173 174 // Output: 175 // map[k4:1] 176 } 177 178 func ExampleListMap_Set() { 179 m := gmap.NewListMap() 180 181 m.Set("key1", "val1") 182 fmt.Println(m) 183 184 // Output: 185 // {"key1":"val1"} 186 } 187 188 func ExampleListMap_Sets() { 189 m := gmap.NewListMap() 190 191 addMap := make(map[interface{}]interface{}) 192 addMap["key1"] = "val1" 193 addMap["key2"] = "val2" 194 addMap["key3"] = "val3" 195 196 m.Sets(addMap) 197 fmt.Println(m) 198 199 // May Output: 200 // {"key1":"val1","key2":"val2","key3":"val3"} 201 } 202 203 func ExampleListMap_Search() { 204 m := gmap.NewListMap() 205 206 m.Set("key1", "val1") 207 208 value, found := m.Search("key1") 209 if found { 210 fmt.Println("find key1 value:", value) 211 } 212 213 value, found = m.Search("key2") 214 if !found { 215 fmt.Println("key2 not find") 216 } 217 218 // Output: 219 // find key1 value: val1 220 // key2 not find 221 } 222 223 func ExampleListMap_Get() { 224 m := gmap.NewListMap() 225 226 m.Set("key1", "val1") 227 228 fmt.Println("key1 value:", m.Get("key1")) 229 fmt.Println("key2 value:", m.Get("key2")) 230 231 // Output: 232 // key1 value: val1 233 // key2 value: <nil> 234 } 235 236 func ExampleListMap_Pop() { 237 var m gmap.ListMap 238 m.Sets(g.MapAnyAny{ 239 "k1": "v1", 240 "k2": "v2", 241 "k3": "v3", 242 "k4": "v4", 243 }) 244 245 fmt.Println(m.Pop()) 246 247 // May Output: 248 // k1 v1 249 } 250 251 func ExampleListMap_Pops() { 252 var m gmap.ListMap 253 m.Sets(g.MapAnyAny{ 254 "k1": "v1", 255 "k2": "v2", 256 "k3": "v3", 257 "k4": "v4", 258 }) 259 fmt.Println(m.Pops(-1)) 260 fmt.Println("size:", m.Size()) 261 262 m.Sets(g.MapAnyAny{ 263 "k1": "v1", 264 "k2": "v2", 265 "k3": "v3", 266 "k4": "v4", 267 }) 268 fmt.Println(m.Pops(2)) 269 fmt.Println("size:", m.Size()) 270 271 // May Output: 272 // map[k1:v1 k2:v2 k3:v3 k4:v4] 273 // size: 0 274 // map[k1:v1 k2:v2] 275 // size: 2 276 } 277 278 func ExampleListMap_GetOrSet() { 279 m := gmap.NewListMap() 280 m.Set("key1", "val1") 281 282 fmt.Println(m.GetOrSet("key1", "NotExistValue")) 283 fmt.Println(m.GetOrSet("key2", "val2")) 284 285 // Output: 286 // val1 287 // val2 288 } 289 290 func ExampleListMap_GetOrSetFunc() { 291 m := gmap.NewListMap() 292 m.Set("key1", "val1") 293 294 fmt.Println(m.GetOrSetFunc("key1", func() interface{} { 295 return "NotExistValue" 296 })) 297 fmt.Println(m.GetOrSetFunc("key2", func() interface{} { 298 return "NotExistValue" 299 })) 300 301 // Output: 302 // val1 303 // NotExistValue 304 } 305 306 func ExampleListMap_GetOrSetFuncLock() { 307 m := gmap.NewListMap() 308 m.Set("key1", "val1") 309 310 fmt.Println(m.GetOrSetFuncLock("key1", func() interface{} { 311 return "NotExistValue" 312 })) 313 fmt.Println(m.GetOrSetFuncLock("key2", func() interface{} { 314 return "NotExistValue" 315 })) 316 317 // Output: 318 // val1 319 // NotExistValue 320 } 321 322 func ExampleListMap_GetVar() { 323 m := gmap.NewListMap() 324 m.Set("key1", "val1") 325 326 fmt.Println(m.GetVar("key1")) 327 fmt.Println(m.GetVar("key2").IsNil()) 328 329 // Output: 330 // val1 331 // true 332 } 333 334 func ExampleListMap_GetVarOrSet() { 335 m := gmap.NewListMap() 336 m.Set("key1", "val1") 337 338 fmt.Println(m.GetVarOrSet("key1", "NotExistValue")) 339 fmt.Println(m.GetVarOrSet("key2", "val2")) 340 341 // Output: 342 // val1 343 // val2 344 } 345 346 func ExampleListMap_GetVarOrSetFunc() { 347 m := gmap.NewListMap() 348 m.Set("key1", "val1") 349 350 fmt.Println(m.GetVarOrSetFunc("key1", func() interface{} { 351 return "NotExistValue" 352 })) 353 fmt.Println(m.GetVarOrSetFunc("key2", func() interface{} { 354 return "NotExistValue" 355 })) 356 357 // Output: 358 // val1 359 // NotExistValue 360 } 361 362 func ExampleListMap_GetVarOrSetFuncLock() { 363 m := gmap.NewListMap() 364 m.Set("key1", "val1") 365 366 fmt.Println(m.GetVarOrSetFuncLock("key1", func() interface{} { 367 return "NotExistValue" 368 })) 369 fmt.Println(m.GetVarOrSetFuncLock("key2", func() interface{} { 370 return "NotExistValue" 371 })) 372 373 // Output: 374 // val1 375 // NotExistValue 376 } 377 378 func ExampleListMap_SetIfNotExist() { 379 var m gmap.ListMap 380 fmt.Println(m.SetIfNotExist("k1", "v1")) 381 fmt.Println(m.SetIfNotExist("k1", "v2")) 382 fmt.Println(m.Map()) 383 384 // Output: 385 // true 386 // false 387 // map[k1:v1] 388 } 389 390 func ExampleListMap_SetIfNotExistFunc() { 391 var m gmap.ListMap 392 fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { 393 return "v1" 394 })) 395 fmt.Println(m.SetIfNotExistFunc("k1", func() interface{} { 396 return "v2" 397 })) 398 fmt.Println(m.Map()) 399 400 // Output: 401 // true 402 // false 403 // map[k1:v1] 404 } 405 406 func ExampleListMap_SetIfNotExistFuncLock() { 407 var m gmap.ListMap 408 fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { 409 return "v1" 410 })) 411 fmt.Println(m.SetIfNotExistFuncLock("k1", func() interface{} { 412 return "v2" 413 })) 414 fmt.Println(m.Map()) 415 416 // Output: 417 // true 418 // false 419 // map[k1:v1] 420 } 421 422 func ExampleListMap_Remove() { 423 var m gmap.ListMap 424 m.Set("k1", "v1") 425 426 fmt.Println(m.Remove("k1")) 427 fmt.Println(m.Remove("k2")) 428 fmt.Println(m.Size()) 429 430 // Output: 431 // v1 432 // <nil> 433 // 0 434 } 435 436 func ExampleListMap_Removes() { 437 var m gmap.ListMap 438 m.Sets(g.MapAnyAny{ 439 "k1": "v1", 440 "k2": "v2", 441 "k3": "v3", 442 "k4": "v4", 443 }) 444 445 removeList := make([]interface{}, 2) 446 removeList = append(removeList, "k1") 447 removeList = append(removeList, "k2") 448 449 m.Removes(removeList) 450 451 fmt.Println(m.Map()) 452 453 // Output: 454 // map[k3:v3 k4:v4] 455 } 456 457 func ExampleListMap_Keys() { 458 var m gmap.ListMap 459 m.Sets(g.MapAnyAny{ 460 "k1": "v1", 461 "k2": "v2", 462 "k3": "v3", 463 "k4": "v4", 464 }) 465 fmt.Println(m.Keys()) 466 467 // May Output: 468 // [k1 k2 k3 k4] 469 } 470 471 func ExampleListMap_Values() { 472 var m gmap.ListMap 473 m.Sets(g.MapAnyAny{ 474 "k1": "v1", 475 "k2": "v2", 476 "k3": "v3", 477 "k4": "v4", 478 }) 479 fmt.Println(m.Values()) 480 481 // May Output: 482 // [v1 v2 v3 v4] 483 } 484 485 func ExampleListMap_Contains() { 486 var m gmap.ListMap 487 m.Sets(g.MapAnyAny{ 488 "k1": "v1", 489 "k2": "v2", 490 "k3": "v3", 491 "k4": "v4", 492 }) 493 494 fmt.Println(m.Contains("k1")) 495 fmt.Println(m.Contains("k5")) 496 497 // Output: 498 // true 499 // false 500 } 501 502 func ExampleListMap_Size() { 503 var m gmap.ListMap 504 m.Sets(g.MapAnyAny{ 505 "k1": "v1", 506 "k2": "v2", 507 "k3": "v3", 508 "k4": "v4", 509 }) 510 511 fmt.Println(m.Size()) 512 513 // Output: 514 // 4 515 } 516 517 func ExampleListMap_IsEmpty() { 518 var m gmap.ListMap 519 fmt.Println(m.IsEmpty()) 520 521 m.Set("k1", "v1") 522 fmt.Println(m.IsEmpty()) 523 524 // Output: 525 // true 526 // false 527 } 528 529 func ExampleListMap_Flip() { 530 var m gmap.ListMap 531 m.Sets(g.MapAnyAny{ 532 "k1": "v1", 533 }) 534 m.Flip() 535 fmt.Println(m.Map()) 536 537 // Output: 538 // map[v1:k1] 539 } 540 541 func ExampleListMap_Merge() { 542 var m1, m2 gmap.ListMap 543 m1.Set("key1", "val1") 544 m2.Set("key2", "val2") 545 m1.Merge(&m2) 546 fmt.Println(m1.Map()) 547 548 // May Output: 549 // map[key1:val1 key2:val2] 550 } 551 552 func ExampleListMap_String() { 553 var m gmap.ListMap 554 m.Sets(g.MapAnyAny{ 555 "k1": "v1", 556 }) 557 558 fmt.Println(m.String()) 559 560 // Output: 561 // {"k1":"v1"} 562 } 563 564 func ExampleListMap_MarshalJSON() { 565 var m gmap.ListMap 566 m.Set("k1", "v1") 567 m.Set("k2", "v2") 568 m.Set("k3", "v3") 569 m.Set("k4", "v4") 570 571 bytes, err := json.Marshal(&m) 572 if err == nil { 573 fmt.Println(gconv.String(bytes)) 574 } 575 576 // Output: 577 // {"k1":"v1","k2":"v2","k3":"v3","k4":"v4"} 578 } 579 580 func ExampleListMap_UnmarshalJSON() { 581 var m gmap.ListMap 582 m.Sets(g.MapAnyAny{ 583 "k1": "v1", 584 "k2": "v2", 585 "k3": "v3", 586 "k4": "v4", 587 }) 588 589 var n gmap.ListMap 590 591 err := json.Unmarshal(gconv.Bytes(m.String()), &n) 592 if err == nil { 593 fmt.Println(n.Map()) 594 } 595 596 // Output: 597 // map[k1:v1 k2:v2 k3:v3 k4:v4] 598 } 599 600 func ExampleListMap_UnmarshalValue() { 601 type User struct { 602 Uid int 603 Name string 604 Pass1 string `gconv:"password1"` 605 Pass2 string `gconv:"password2"` 606 } 607 608 var ( 609 m gmap.AnyAnyMap 610 user = User{ 611 Uid: 1, 612 Name: "john", 613 Pass1: "123", 614 Pass2: "456", 615 } 616 ) 617 if err := gconv.Scan(user, &m); err == nil { 618 fmt.Printf("%#v", m.Map()) 619 } 620 621 // Output: 622 // map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"} 623 }