1
0
mirror of https://github.com/nitezs/sub2clash.git synced 2024-12-24 11:54:41 -05:00
sub2clash/utils/template.go
2023-09-23 01:31:04 +08:00

35 lines
615 B
Go

package utils
import (
"errors"
"io"
"os"
"path/filepath"
)
// LoadTemplate 加载模板
// templates 模板文件名
func LoadTemplate(template string) ([]byte, error) {
tPath := filepath.Join("templates", template)
if _, err := os.Stat(tPath); err == nil {
file, err := os.Open(tPath)
if err != nil {
return nil, err
}
defer func(file *os.File) {
if file != nil {
_ = file.Close()
}
}(file)
result, err := io.ReadAll(file)
if err != nil {
return nil, err
}
if err != nil {
return nil, err
}
return result, nil
}
return nil, errors.New("模板文件不存在")
}