• golang 简易的键值配置包


    type Setting struct {
    	path string
    	prefix string
    	set map[string]interface{}
    }
    
    func NewSetting(file string) *Setting{
    	s := Setting{}
    	s.path = file
    	s.set = make(map[string]interface{})
    	return &s
    }
    
    func (s *Setting)load(){
    	s.clear()
    
    	f,err := os.Open(s.path)
    	if err != nil{
    		fmt.Println(err)
    		return
    	}
    
    	//扫描行并分割处理
    	scanner := bufio.NewScanner(f)
    	for scanner.Scan() {
    		text := scanner.Text()
    		if len(text) < 3 || !strings.Contains(text,"="){
    			continue
    		}
    
    		stringList := strings.Split(text,"=",)
    		//fmt.Println(reflect.TypeOf(value))
    		s.setValue(strings.TrimSpace(stringList[0]),strings.TrimSpace(stringList[1]))
    		//mt.Println(scanner.Text())
    	}
    
    	err = scanner.Err()
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    
    func (s *Setting)loadByFile(file string){
    	s.path = file
    	s.load()
    }
    
    func (s *Setting)save(){
    	f,err := os.Create(s.path)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	defer f.Close()
    	for k,v:= range s.set {
    		switch v.(type) {
    		case int:
    			f.WriteString(fmt.Sprintf("%s=%d
    ",k,v))
    		case float64:
    			f.WriteString(fmt.Sprintf("%s=%f
    ",k,v))
    		case string:
    			//f.WriteString(fmt.Sprintf("%s="%s"
    ",k,v))
    			f.WriteString(fmt.Sprintf("%s=%s
    ",k,v))
    		case bool:
    			f.WriteString(fmt.Sprintf("%s=%t
    ",k,v))
    		default:
    			f.WriteString(fmt.Sprintf("%s=%v
    ",k,v))
    		}
    	}
    }
    
    func (s *Setting)saveByFile(file string){
    	s.path = file
    	s.save()
    }
    
    func (s *Setting)setValue(key string,value interface{}){
    	s.set[key] = value
    }
    
    func (s *Setting)value(key string)interface{}{
    	return s.set[key]
    }
    
    func (s *Setting)valueByDefault(key string,value interface{})interface{}{
    	if s.contains(key) {
    		return s.value(key)
    	}
    	return value
    }
    
    func (s *Setting)clear(){
    	for k := range s.set{
    		delete(s.set,k)
    	}
    }
    
    func (s *Setting)beginGroup(prefix string){
    	s.prefix = prefix
    }
    
    func (s *Setting)endGroup(){
    	s.prefix = ""
    }
    
    func (s *Setting)contains(key string)bool{
    	if _,find := s.set[key];find{
    		return true
    	}else{
    		return false
    	}
    }
    
    func (s *Setting)fileName()string{
    	return s.path
    }
    
    func (s *Setting)remove(key string){
    	delete(s.set,key)
    }
    
    func (s *Setting)sync(){
    
    }
    
    func (s *Setting)allKeys()[]string{
    	keys := make([]string,len(s.set))
    	for k:= range s.set{
    		keys = append(keys,k)
    	}
    	return keys
    }
    
  • 相关阅读:
    矩阵树(Matrix-Tree)定理精华
    【面试题flex】关于弹性盒子中 flex: 0 1 auto 的一系列问题
    纯函数-Javascript
    uniapp 低功耗蓝牙使用(经典蓝牙用native.js)
    uniapp截屏
    unicloud数据聚合处理
    uniapp的post请求失效问题
    unicloud链表查询
    uni-id的使用
    坐标转换
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/12409389.html
Copyright © 2020-2023  润新知