GOLANG读取配置文件(简易版)
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
)
var properties = make(map[string]string)
func init() {
srcFile, err := os.OpenFile("./application.properties", os.O_RDONLY, 0666)
defer srcFile.Close()
if err != nil {
fmt.Println("The file not exits.")
} else {
srcReader := bufio.NewReader(srcFile)
for {
str, err := srcReader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
}
if 0 == len(str) || str == "\n" {
continue
}
properties[strings.Replace(strings.Split(str, "=")[0], " ", "", -1)] = strings.Replace(strings.Split(str, "=")[1], " ", "", -1)
}
}
}
func main() {
fmt.Print(properties["host"])
}