I'm trying to parse CSS files in which variables can be injected that are defined in a config file. Currently the function does:
- Opens the file based on the given path argument
- Parses it the file's content
- Executes the template by injecting the config variable
- Writes the rendered content to the console instead into the original file
func parse(path string) {
f, err := ioutil.ReadFile(path)
if err != nil {
log.Print(err)
return
}
// Parse requires a string
t, err := template.New("css").Parse(string(f))
if err != nil {
log.Print(err)
return
}
// A sample config
config := map[string]string {
"textColor": "#abcdef",
"linkColorHover": "#ffaacc",
}
// Execute needs some sort of io.Writer
err = t.Execute(os.Stdout, config)
if err != nil {
log.Print("Can't execute ", path)
}
}
My problem is that template.Parse()
requires the content as string and template.Execute()
an io.Writer
as argument. I tried to open the file with os.Open()
which returns a file object that implements the io.Writer
interface. But how can I get the file's content as a string from such a file object in order to use it with Parse()
?