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