代码拉取完成,页面将自动刷新
package eztools
import (
"encoding/xml"
"io/ioutil"
"os"
"path/filepath"
)
// FileWrite writes input bytes to file
func FileWrite(file string, bytes []byte, createIfNeeded bool) error {
if !createIfNeeded {
if _, err := os.Stat(file); err != nil {
return err
}
}
const PERMISSIONS = 0644
return ioutil.WriteFile(file, bytes, PERMISSIONS)
}
// XMLWrite writes input structure to file
// this reformats the file with indent provided
func XMLWrite(file string, data interface{}, createIfNeeded bool, indent string) error {
bytes, err := xml.MarshalIndent(data, "", indent)
if err != nil {
return err
}
var PREFIX = []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
return FileWrite(file, append(PREFIX, bytes...), createIfNeeded)
}
// XMLWriteNoCreate writes config file from input structure
// by a full path (with .xml extension)
// this reformats the file with indent provided
func XMLWriteNoCreate(file string, cfg interface{}, indent string) error {
return XMLWrite(file, cfg, false, indent)
}
// XMLRead reads file into input structure
func XMLRead(file string, data interface{}, _ bool) error {
if _, err := os.Stat(file); err != nil {
return err
}
bytes, err := ioutil.ReadFile(file)
if err != nil {
return err
}
return xml.Unmarshal(bytes, data)
}
// XMLsReadDefault reads a config file
func XMLsReadDefault(path, file string, cfg interface{},
createIfNeeded bool) (
pathFound string, err error) {
if len(path) > 0 {
err = XMLRead(path, cfg, createIfNeeded)
if err == nil {
return path, err
}
}
if len(file) < 1 {
return "", ErrNoValidResults
}
home, _ := os.UserHomeDir()
cfgPaths := [...]string{".", home}
for _, path1 := range cfgPaths {
pathFound = filepath.Join(path1, file+".xml")
err = XMLRead(pathFound, cfg, createIfNeeded)
if err == nil {
return
}
}
return "", ErrNoValidResults
}
// XMLsReadDefaultNoCreate reads config file into input structure from
// given path, or given file name (plus .xml) under current dir or home dir
// returns full file name with path
func XMLsReadDefaultNoCreate(path, file string, cfg interface{}) (string, error) {
return XMLsReadDefault(path, file, cfg, false)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。