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