github.com/gogf/gf/v2@v2.7.4/container/gvar/gvar_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 this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gvar_test 8 9 import ( 10 "fmt" 11 12 "github.com/gogf/gf/v2/container/gvar" 13 "github.com/gogf/gf/v2/frame/g" 14 "github.com/gogf/gf/v2/internal/json" 15 ) 16 17 // New 18 func ExampleVarNew() { 19 v := gvar.New(400) 20 fmt.Println(v) 21 22 // Output: 23 // 400 24 } 25 26 // Clone 27 func ExampleVar_Clone() { 28 tmp := "fisrt hello" 29 v := gvar.New(tmp) 30 g.DumpWithType(v.Clone()) 31 fmt.Println(v == v.Clone()) 32 33 // Output: 34 // *gvar.Var(11) "fisrt hello" 35 // false 36 } 37 38 // Set 39 func ExampleVar_Set() { 40 var v = gvar.New(100.00) 41 g.Dump(v.Set(200.00)) 42 g.Dump(v) 43 44 // Output: 45 // 100 46 // "200" 47 } 48 49 // Val 50 func ExampleVar_Val() { 51 var v = gvar.New(100.00) 52 g.DumpWithType(v.Val()) 53 54 // Output: 55 // float64(100) 56 } 57 58 // Interface 59 func ExampleVar_Interface() { 60 var v = gvar.New(100.00) 61 g.DumpWithType(v.Interface()) 62 63 // Output: 64 // float64(100) 65 } 66 67 // Bytes 68 func ExampleVar_Bytes() { 69 var v = gvar.New("GoFrame") 70 g.DumpWithType(v.Bytes()) 71 72 // Output: 73 // []byte(7) "GoFrame" 74 } 75 76 // String 77 func ExampleVar_String() { 78 var v = gvar.New("GoFrame") 79 g.DumpWithType(v.String()) 80 81 // Output: 82 // string(7) "GoFrame" 83 } 84 85 // Bool 86 func ExampleVar_Bool() { 87 var v = gvar.New(true) 88 g.DumpWithType(v.Bool()) 89 90 // Output: 91 // bool(true) 92 } 93 94 // Int 95 func ExampleVar_Int() { 96 var v = gvar.New(-1000) 97 g.DumpWithType(v.Int()) 98 99 // Output: 100 // int(-1000) 101 } 102 103 // Uint 104 func ExampleVar_Uint() { 105 var v = gvar.New(1000) 106 g.DumpWithType(v.Uint()) 107 108 // Output: 109 // uint(1000) 110 } 111 112 // Float32 113 func ExampleVar_Float32() { 114 var price = gvar.New(100.00) 115 g.DumpWithType(price.Float32()) 116 117 // Output: 118 // float32(100) 119 } 120 121 // Time 122 func ExampleVar_Time() { 123 var v = gvar.New("2021-11-11 00:00:00") 124 g.DumpWithType(v.Time()) 125 126 // Output: 127 // time.Time(29) "2021-11-11 00:00:00 +0800 CST" 128 } 129 130 // GTime 131 func ExampleVar_GTime() { 132 var v = gvar.New("2021-11-11 00:00:00") 133 g.DumpWithType(v.GTime()) 134 135 // Output: 136 // *gtime.Time(19) "2021-11-11 00:00:00" 137 } 138 139 // Duration 140 func ExampleVar_Duration() { 141 var v = gvar.New("300s") 142 g.DumpWithType(v.Duration()) 143 144 // Output: 145 // time.Duration(4) "5m0s" 146 } 147 148 // MarshalJSON 149 func ExampleVar_MarshalJSON() { 150 testMap := g.Map{ 151 "code": "0001", 152 "name": "Golang", 153 "count": 10, 154 } 155 156 var v = gvar.New(testMap) 157 res, err := json.Marshal(&v) 158 if err != nil { 159 panic(err) 160 } 161 g.DumpWithType(res) 162 163 // Output: 164 // []byte(42) "{"code":"0001","count":10,"name":"Golang"}" 165 } 166 167 // UnmarshalJSON 168 func ExampleVar_UnmarshalJSON() { 169 tmp := []byte(`{ 170 "Code": "0003", 171 "Name": "Golang Book3", 172 "Quantity": 3000, 173 "Price": 300, 174 "OnSale": true 175 }`) 176 var v = gvar.New(map[string]interface{}{}) 177 if err := json.Unmarshal(tmp, &v); err != nil { 178 panic(err) 179 } 180 181 g.Dump(v) 182 183 // Output: 184 // "{\"Code\":\"0003\",\"Name\":\"Golang Book3\",\"OnSale\":true,\"Price\":300,\"Quantity\":3000}" 185 } 186 187 // UnmarshalValue 188 func ExampleVar_UnmarshalValue() { 189 tmp := g.Map{ 190 "code": "00002", 191 "name": "GoFrame", 192 "price": 100, 193 "sale": true, 194 } 195 196 var v = gvar.New(map[string]interface{}{}) 197 if err := v.UnmarshalValue(tmp); err != nil { 198 panic(err) 199 } 200 g.Dump(v) 201 202 // Output: 203 // "{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}" 204 } 205 206 // IsNil 207 func ExampleVar_IsNil() { 208 g.Dump(gvar.New(0).IsNil()) 209 g.Dump(gvar.New(0.1).IsNil()) 210 // true 211 g.Dump(gvar.New(nil).IsNil()) 212 g.Dump(gvar.New("").IsNil()) 213 214 // Output: 215 // false 216 // false 217 // true 218 // false 219 } 220 221 // IsEmpty 222 func ExampleVar_IsEmpty() { 223 g.Dump(gvar.New(0).IsEmpty()) 224 g.Dump(gvar.New(nil).IsEmpty()) 225 g.Dump(gvar.New("").IsEmpty()) 226 g.Dump(gvar.New(g.Map{"k": "v"}).IsEmpty()) 227 228 // Output: 229 // true 230 // true 231 // true 232 // false 233 } 234 235 // IsInt 236 func ExampleVar_IsInt() { 237 g.Dump(gvar.New(0).IsInt()) 238 g.Dump(gvar.New(0.1).IsInt()) 239 g.Dump(gvar.New(nil).IsInt()) 240 g.Dump(gvar.New("").IsInt()) 241 242 // Output: 243 // true 244 // false 245 // false 246 // false 247 } 248 249 // IsUint 250 func ExampleVar_IsUint() { 251 g.Dump(gvar.New(0).IsUint()) 252 g.Dump(gvar.New(uint8(8)).IsUint()) 253 g.Dump(gvar.New(nil).IsUint()) 254 255 // Output: 256 // false 257 // true 258 // false 259 } 260 261 // IsFloat 262 func ExampleVar_IsFloat() { 263 g.Dump(g.NewVar(uint8(8)).IsFloat()) 264 g.Dump(g.NewVar(float64(8)).IsFloat()) 265 g.Dump(g.NewVar(0.1).IsFloat()) 266 267 // Output: 268 // false 269 // true 270 // true 271 } 272 273 // IsSlice 274 func ExampleVar_IsSlice() { 275 g.Dump(g.NewVar(0).IsSlice()) 276 g.Dump(g.NewVar(g.Slice{0}).IsSlice()) 277 278 // Output: 279 // false 280 // true 281 } 282 283 // IsMap 284 func ExampleVar_IsMap() { 285 g.Dump(g.NewVar(0).IsMap()) 286 g.Dump(g.NewVar(g.Map{"k": "v"}).IsMap()) 287 g.Dump(g.NewVar(g.Slice{}).IsMap()) 288 289 // Output: 290 // false 291 // true 292 // false 293 } 294 295 // IsStruct 296 func ExampleVar_IsStruct() { 297 g.Dump(g.NewVar(0).IsStruct()) 298 g.Dump(g.NewVar(g.Map{"k": "v"}).IsStruct()) 299 300 a := struct{}{} 301 g.Dump(g.NewVar(a).IsStruct()) 302 g.Dump(g.NewVar(&a).IsStruct()) 303 304 // Output: 305 // false 306 // false 307 // true 308 // true 309 } 310 311 // ListItemValues 312 func ExampleVar_ListItemValues() { 313 var goods1 = g.List{ 314 g.Map{"id": 1, "price": 100.00}, 315 g.Map{"id": 2, "price": 0}, 316 g.Map{"id": 3, "price": nil}, 317 } 318 var v = gvar.New(goods1) 319 fmt.Println(v.ListItemValues("id")) 320 fmt.Println(v.ListItemValues("price")) 321 322 // Output: 323 // [1 2 3] 324 // [100 0 <nil>] 325 } 326 327 // ListItemValuesUnique 328 func ExampleVar_ListItemValuesUnique() { 329 var ( 330 goods1 = g.List{ 331 g.Map{"id": 1, "price": 100.00}, 332 g.Map{"id": 2, "price": 100.00}, 333 g.Map{"id": 3, "price": nil}, 334 } 335 v = gvar.New(goods1) 336 ) 337 338 fmt.Println(v.ListItemValuesUnique("id")) 339 fmt.Println(v.ListItemValuesUnique("price")) 340 341 // Output: 342 // [1 2 3] 343 // [100 <nil>] 344 } 345 346 func ExampleVar_Struct() { 347 params1 := g.Map{ 348 "uid": 1, 349 "Name": "john", 350 } 351 v := gvar.New(params1) 352 type tartget struct { 353 Uid int 354 Name string 355 } 356 t := new(tartget) 357 if err := v.Struct(&t); err != nil { 358 panic(err) 359 } 360 g.Dump(t) 361 362 // Output: 363 // { 364 // Uid: 1, 365 // Name: "john", 366 // } 367 } 368 369 func ExampleVar_Structs() { 370 paramsArray := []g.Map{} 371 params1 := g.Map{ 372 "uid": 1, 373 "Name": "golang", 374 } 375 params2 := g.Map{ 376 "uid": 2, 377 "Name": "java", 378 } 379 380 paramsArray = append(paramsArray, params1, params2) 381 v := gvar.New(paramsArray) 382 type tartget struct { 383 Uid int 384 Name string 385 } 386 var t []tartget 387 if err := v.Structs(&t); err != nil { 388 panic(err) 389 } 390 g.DumpWithType(t) 391 392 // Output: 393 // []gvar_test.tartget(2) [ 394 // gvar_test.tartget(2) { 395 // Uid: int(1), 396 // Name: string(6) "golang", 397 // }, 398 // gvar_test.tartget(2) { 399 // Uid: int(2), 400 // Name: string(4) "java", 401 // }, 402 // ] 403 } 404 405 // Ints 406 func ExampleVar_Ints() { 407 var ( 408 arr = []int{1, 2, 3, 4, 5} 409 obj = gvar.New(arr) 410 ) 411 412 fmt.Println(obj.Ints()) 413 414 // Output: 415 // [1 2 3 4 5] 416 } 417 418 // Int64s 419 func ExampleVar_Int64s() { 420 var ( 421 arr = []int64{1, 2, 3, 4, 5} 422 obj = gvar.New(arr) 423 ) 424 425 fmt.Println(obj.Int64s()) 426 427 // Output: 428 // [1 2 3 4 5] 429 } 430 431 // Uints 432 func ExampleVar_Uints() { 433 var ( 434 arr = []uint{1, 2, 3, 4, 5} 435 obj = gvar.New(arr) 436 ) 437 fmt.Println(obj.Uints()) 438 439 // Output: 440 // [1 2 3 4 5] 441 } 442 443 // Uint64s 444 func ExampleVar_Uint64s() { 445 var ( 446 arr = []uint64{1, 2, 3, 4, 5} 447 obj = gvar.New(arr) 448 ) 449 450 fmt.Println(obj.Uint64s()) 451 452 // Output: 453 // [1 2 3 4 5] 454 } 455 456 // Floats 457 func ExampleVar_Floats() { 458 var ( 459 arr = []float64{1, 2, 3, 4, 5} 460 obj = gvar.New(arr) 461 ) 462 463 fmt.Println(obj.Floats()) 464 465 // Output: 466 // [1 2 3 4 5] 467 } 468 469 // Float32s 470 func ExampleVar_Float32s() { 471 var ( 472 arr = []float32{1, 2, 3, 4, 5} 473 obj = gvar.New(arr) 474 ) 475 476 fmt.Println(obj.Float32s()) 477 478 // Output: 479 // [1 2 3 4 5] 480 } 481 482 // Float64s 483 func ExampleVar_Float64s() { 484 var ( 485 arr = []float64{1, 2, 3, 4, 5} 486 obj = gvar.New(arr) 487 ) 488 489 fmt.Println(obj.Float64s()) 490 491 // Output: 492 // [1 2 3 4 5] 493 } 494 495 // Strings 496 func ExampleVar_Strings() { 497 var ( 498 arr = []string{"GoFrame", "Golang"} 499 obj = gvar.New(arr) 500 ) 501 fmt.Println(obj.Strings()) 502 503 // Output: 504 // [GoFrame Golang] 505 } 506 507 // Interfaces 508 func ExampleVar_Interfaces() { 509 var ( 510 arr = []string{"GoFrame", "Golang"} 511 obj = gvar.New(arr) 512 ) 513 514 fmt.Println(obj.Interfaces()) 515 516 // Output: 517 // [GoFrame Golang] 518 } 519 520 // Slice 521 func ExampleVar_Slice() { 522 var ( 523 arr = []string{"GoFrame", "Golang"} 524 obj = gvar.New(arr) 525 ) 526 527 fmt.Println(obj.Slice()) 528 529 // Output: 530 // [GoFrame Golang] 531 } 532 533 // Array 534 func ExampleVar_Array() { 535 var ( 536 arr = []string{"GoFrame", "Golang"} 537 obj = gvar.New(arr) 538 ) 539 fmt.Println(obj.Array()) 540 541 // Output: 542 // [GoFrame Golang] 543 } 544 545 // Vars 546 func ExampleVar_Vars() { 547 var ( 548 arr = []string{"GoFrame", "Golang"} 549 obj = gvar.New(arr) 550 ) 551 552 fmt.Println(obj.Vars()) 553 554 // Output: 555 // [GoFrame Golang] 556 } 557 558 // Map 559 func ExampleVar_Map() { 560 var ( 561 m = g.Map{"id": 1, "price": 100.00} 562 v = gvar.New(m) 563 res = v.Map() 564 ) 565 566 fmt.Println(res["id"], res["price"]) 567 568 // Output: 569 // 1 100 570 } 571 572 // MapStrAny 573 func ExampleVar_MapStrAny() { 574 var ( 575 m1 = g.Map{"id": 1, "price": 100} 576 v = gvar.New(m1) 577 v2 = v.MapStrAny() 578 ) 579 580 fmt.Println(v2["price"], v2["id"]) 581 582 // Output: 583 // 100 1 584 } 585 586 // MapStrStr 587 func ExampleVar_MapStrStr() { 588 var ( 589 m1 = g.Map{"id": 1, "price": 100} 590 v = gvar.New(m1) 591 v2 = v.MapStrStr() 592 ) 593 594 fmt.Println(v2["price"] + "$") 595 596 // Output: 597 // 100$ 598 } 599 600 // MapStrVar 601 func ExampleVar_MapStrVar() { 602 var ( 603 m1 = g.Map{"id": 1, "price": 100} 604 v = gvar.New(m1) 605 v2 = v.MapStrVar() 606 ) 607 608 fmt.Println(v2["price"].Float64() * 100) 609 610 // Output: 611 // 10000 612 } 613 614 // MapDeep 615 func ExampleVar_MapDeep() { 616 var ( 617 m1 = g.Map{"id": 1, "price": 100} 618 m2 = g.Map{"product": m1} 619 v = gvar.New(m2) 620 v2 = v.MapDeep() 621 ) 622 623 fmt.Println(v2["product"]) 624 625 // Output: 626 // map[id:1 price:100] 627 } 628 629 // MapStrStrDeep 630 func ExampleVar_MapStrStrDeep() { 631 var ( 632 m1 = g.Map{"id": 1, "price": 100} 633 m2 = g.Map{"product": m1} 634 v = gvar.New(m2) 635 v2 = v.MapStrStrDeep() 636 ) 637 638 fmt.Println(v2["product"]) 639 640 // Output: 641 // {"id":1,"price":100} 642 } 643 644 // MapStrVarDeep 645 func ExampleVar_MapStrVarDeep() { 646 var ( 647 m1 = g.Map{"id": 1, "price": 100} 648 m2 = g.Map{"product": m1} 649 m3 = g.Map{} 650 v = gvar.New(m2) 651 v2 = v.MapStrVarDeep() 652 v3 = gvar.New(m3).MapStrVarDeep() 653 ) 654 655 fmt.Println(v2["product"]) 656 fmt.Println(v3) 657 658 // Output: 659 // {"id":1,"price":100} 660 // map[] 661 } 662 663 // Maps 664 func ExampleVar_Maps() { 665 var m = gvar.New(g.ListIntInt{g.MapIntInt{0: 100, 1: 200}, g.MapIntInt{0: 300, 1: 400}}) 666 fmt.Printf("%#v", m.Maps()) 667 668 // Output: 669 // []map[string]interface {}{map[string]interface {}{"0":100, "1":200}, map[string]interface {}{"0":300, "1":400}} 670 } 671 672 // MapsDeep 673 func ExampleVar_MapsDeep() { 674 var ( 675 p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}} 676 p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}} 677 v = gvar.New(g.ListStrAny{p1, p2}) 678 v2 = v.MapsDeep() 679 ) 680 681 fmt.Printf("%#v", v2) 682 683 // Output: 684 // []map[string]interface {}{map[string]interface {}{"product":map[string]interface {}{"id":1, "price":100}}, map[string]interface {}{"product":map[string]interface {}{"id":2, "price":200}}} 685 } 686 687 // MapToMap 688 func ExampleVar_MapToMap() { 689 var ( 690 m1 = gvar.New(g.MapIntInt{0: 100, 1: 200}) 691 m2 = g.MapStrStr{} 692 ) 693 694 err := m1.MapToMap(&m2) 695 if err != nil { 696 panic(err) 697 } 698 699 fmt.Printf("%#v", m2) 700 701 // Output: 702 // map[string]string{"0":"100", "1":"200"} 703 } 704 705 // MapToMaps 706 func ExampleVar_MapToMaps() { 707 var ( 708 p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}} 709 p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}} 710 v = gvar.New(g.ListStrAny{p1, p2}) 711 v2 []g.MapStrStr 712 ) 713 714 err := v.MapToMaps(&v2) 715 if err != nil { 716 panic(err) 717 } 718 fmt.Printf("%#v", v2) 719 720 // Output: 721 // []map[string]string{map[string]string{"product":"{\"id\":1,\"price\":100}"}, map[string]string{"product":"{\"id\":2,\"price\":200}"}} 722 } 723 724 // MapToMapsDeep 725 func ExampleVar_MapToMapsDeep() { 726 var ( 727 p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}} 728 p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}} 729 v = gvar.New(g.ListStrAny{p1, p2}) 730 v2 []g.MapStrStr 731 ) 732 733 err := v.MapToMapsDeep(&v2) 734 if err != nil { 735 panic(err) 736 } 737 fmt.Printf("%#v", v2) 738 739 // Output: 740 // []map[string]string{map[string]string{"product":"{\"id\":1,\"price\":100}"}, map[string]string{"product":"{\"id\":2,\"price\":200}"}} 741 } 742 743 // Scan 744 func ExampleVar_Scan() { 745 type Student struct { 746 Id *g.Var 747 Name *g.Var 748 Scores *g.Var 749 } 750 var ( 751 s Student 752 m = g.Map{ 753 "Id": 1, 754 "Name": "john", 755 "Scores": []int{100, 99, 98}, 756 } 757 ) 758 v := gvar.New(m) 759 if err := v.Scan(&s); err == nil { 760 g.DumpWithType(s) 761 } 762 763 // Output: 764 // gvar_test.Student(3) { 765 // Id: *gvar.Var(1) "1", 766 // Name: *gvar.Var(4) "john", 767 // Scores: *gvar.Var(11) "[100,99,98]", 768 // } 769 }