github.com/gogf/gf@v1.16.9/os/gcfg/gcfg_z_example_pattern_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 gcfg_test
     8  
     9  import (
    10  	"fmt"
    11  	"github.com/gogf/gf/frame/g"
    12  	"github.com/gogf/gf/internal/intlog"
    13  	"github.com/gogf/gf/os/gcfg"
    14  )
    15  
    16  func Example_mapSliceChange() {
    17  	intlog.SetEnabled(false)
    18  	defer intlog.SetEnabled(true)
    19  	// For testing/example only.
    20  	content := `{"map":{"key":"value"}, "slice":[59,90]}`
    21  	gcfg.SetContent(content)
    22  	defer gcfg.RemoveContent()
    23  
    24  	m := g.Cfg().GetMap("map")
    25  	fmt.Println(m)
    26  
    27  	// Change the key-value pair.
    28  	m["key"] = "john"
    29  
    30  	// It changes the underlying key-value pair.
    31  	fmt.Println(g.Cfg().GetMap("map"))
    32  
    33  	s := g.Cfg().GetArray("slice")
    34  	fmt.Println(s)
    35  
    36  	// Change the value of specified index.
    37  	s[0] = 100
    38  
    39  	// It changes the underlying slice.
    40  	fmt.Println(g.Cfg().GetArray("slice"))
    41  
    42  	// output:
    43  	// map[key:value]
    44  	// map[key:john]
    45  	// [59 90]
    46  	// [100 90]
    47  }