代码拉取完成,页面将自动刷新
package resource
import (
"bytes"
"encoding/json"
"github.com/bitly/go-simplejson"
"gopkg.in/yaml.v3"
"io/ioutil"
"path/filepath"
)
type format string
const (
formatOther format = "other"
formatJson format = "json"
formatYaml format = "yaml"
)
func read2Json(path string) (*simplejson.Json, error) {
_bytes, e := ioutil.ReadFile(path)
if e != nil {
return nil, e
}
return simplejson.NewJson(_bytes)
}
func loadLocalFile(path string, target interface{}) error {
_bytes, e := ioutil.ReadFile(path)
if e != nil {
return e
}
fm := determineFormat(path)
if fm == formatJson {
d := json.NewDecoder(bytes.NewReader(_bytes))
d.UseNumber()
return d.Decode(target)
}
return unmarshaler(determineFormat(path))(_bytes, target)
}
func determineFormat(path string) format {
switch filepath.Ext(path) {
case ".json":
return formatJson
case ".yaml", ".yml":
return formatYaml
}
return formatOther
}
func unmarshaler(f format) func([]byte, interface{}) error {
switch f {
case formatJson:
return json.Unmarshal
case formatYaml:
return yaml.Unmarshal
}
return func(bytes []byte, i interface{}) error {
return nil
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。