Go利用反射实现一个ini文件的解析器程序

编程入门 行业动态 更新时间:2024-10-26 04:28:11

Go利用<a href=https://www.elefans.com/category/jswz/34/1765951.html style=反射实现一个ini文件的解析器程序"/>

Go利用反射实现一个ini文件的解析器程序

package mainimport ("bufio"  // 逐行读取配置文件"fmt""log""os""reflect""strconv""strings"
)type Config struct {	// 定义配置结构体Section1 Section1 `ini:"section1"`	// 嵌套结构体1Section2 Section2 `ini:"section2"`	// 嵌套结构体2
}type Section1 struct {Key1 string `ini:"key1"`Key2 int    `ini:"key2"`
}type Section2 struct {Key3 float64 `ini:"key3"`Key4 bool    `ini:"key4"`
}// fieldName 函数返回一个匹配函数,用于在结构体中查找字段名与给定字段名相同的字段
func fieldName(fieldName string) func(string) bool {return func(tag string) bool {return strings.EqualFold(tag, fieldName)	// 调用strings.EqualFold 函数进行不区分大小写匹配}
}func main() {file, err := os.Open("config.ini")if err != nil {log.Fatalf("Failed to open file: %v", err)}defer file.Close()cfg := Config{}scanner := bufio.NewScanner(file)currentSection := ""v := reflect.ValueOf(&cfg).Elem()for scanner.Scan() {line := strings.TrimSpace(scanner.Text())if line == "" || strings.HasPrefix(line, "#") {continue}if strings.HasPrefix(line, "[") && strings.HasSuffix(line, "]") {currentSection = strings.Trim(line, "[]")sectionField := v.FieldByNameFunc(fieldName(currentSection))if !sectionField.IsValid() {log.Printf("Invalid section: %s", currentSection)}} else {parts := strings.SplitN(line, "=", 2)if len(parts) != 2 {log.Printf("Invalid key-value pair: %s", line)continue}key := strings.TrimSpace(parts[0])value := strings.TrimSpace(parts[1])if currentSection == "" {log.Printf("Key-value pair found outside of any section: %s", line)continue}sectionField := v.FieldByNameFunc(fieldName(currentSection))if !sectionField.IsValid() {log.Printf("Invalid section: %s", currentSection)continue}field := sectionField.FieldByNameFunc(fieldName(key))if !field.IsValid() {log.Printf("Invalid key: %s", key)continue}switch field.Kind() {case reflect.String:field.SetString(value)case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:intValue, err := strconv.ParseInt(value, 10, 64)if err != nil {log.Printf("Failed to parse int value for key %s: %v", key, err)continue}field.SetInt(intValue)case reflect.Float32, reflect.Float64:floatValue, err := strconv.ParseFloat(value, 64)if err != nil {log.Printf("Failed to parse float value for key %s: %v", key, err)continue}field.SetFloat(floatValue)case reflect.Bool:boolValue, err := strconv.ParseBool(value)if err != nil {log.Printf("Failed to parse bool value for key %s: %v", key, err)continue}field.SetBool(boolValue)default:log.Printf("Unsupported field type for key %s", key)}}}if err := scanner.Err(); err != nil {log.Fatalf("Error while scanning file: %v", err)}fmt.Printf("%+v\n", cfg)
}
[Section1]
key1 = value1
key2 = 100[Section2]
key3 = 3.14
key4 = true

更多推荐

Go利用反射实现一个ini文件的解析器程序

本文发布于:2023-11-15 01:24:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1591209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:反射   文件   程序   ini

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!