github.com/zhongdalu/gf@v1.0.0/g/os/gcfg/gcfg_instance.go (about)

     1  // Copyright 2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  package gcfg
     8  
     9  import (
    10  	"github.com/zhongdalu/gf/g/container/gmap"
    11  )
    12  
    13  const (
    14  	// Default group name for instance usage.
    15  	DEFAULT_GROUP_NAME = "default"
    16  )
    17  
    18  var (
    19  	// Instances map.
    20  	instances = gmap.NewStrAnyMap()
    21  )
    22  
    23  // Instance returns an instance of Config with default settings.
    24  // The parameter <name> is the name for the instance.
    25  func Instance(name ...string) *Config {
    26  	key := DEFAULT_GROUP_NAME
    27  	if len(name) > 0 {
    28  		key = name[0]
    29  	}
    30  	return instances.GetOrSetFuncLock(key, func() interface{} {
    31  		return New()
    32  	}).(*Config)
    33  }