• Go操作ini文件


    除了采用json,yaml等格式之外,常用的配置文件还有ini格式的。

     1 cfg, err := ini.Load(fyPath + "\ServerSystem.ini") // need confirm which ini file.
     2     if err != nil {
     3         fmt.Printf("Fail to read file: %v", err)
     4         fmt.Fprintf(w, "{"msg":"%s"}", err.Error())
     5         return false, err
     6     }
     7     // sa password write by clear text
     8     clFlag := cfg.Section("DBServer").Key("User").String()
     9     // rs, _ := utf8.DecodeRuneInString(clFlag)
    10     utf8 := mahonia.NewDecoder("gbk").ConvertString(clFlag)
    11     fmt.Println(utf8)
    12     sc, _ := cfg.NewSection("abc")
    13     sk, _ := sc.NewKey("test", mahonia.NewEncoder("gbk").ConvertString("1ar三郎123")) // ini file is ansi code.so convert...
    14     cfg.Section("DBServer").Key("Name").SetValue(conf.ServerIP)
    15     cfg.Section("DBServer").Key("Key").SetValue(conf.Sapass) // post sapass maybe encrypted,so need decrypt perhaps first,then write to ini file.
    16     cfg.SaveTo(fyPath + "\ServerSystem.ini")
    17     fmt.Println(sk)
    18     // D7 platform need web port to check,so write ServCfg.ini to save webport and make active flag.
    19     cfg, err = ini.Load(fyPath + "\ServCfg.ini")
    20     cfg.Section("Serv").Key("WebPort").SetValue(strconv.Itoa(conf.Httpport))
    21     cfg.Section("Serv").Key("WebActive").SetValue(strconv.FormatBool(conf.Remember)) // need confirm active key's name.
    22     cfg.SaveTo(fyPath + "\ServCfg.ini")

    使用到了

    "github.com/axgle/mahonia"
    "github.com/go-ini/ini"
    两个包。
    一定注意编码格式,避免乱码。
  • 相关阅读:
    昨天又学到了dp
    LeetCode面试题17.13
    leetcode971
    今天又一题,单调队列leetcode862
    今天学到了一招,LeetCode863
    今天一道简单题,数组排序LeetCode973
    es面试题
    es的常用配置
    es基础一张图
    JMeter中引入外部的JAR包给bean shell 使用
  • 原文地址:https://www.cnblogs.com/codingnote/p/11265419.html
Copyright © 2020-2023  润新知