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