Skip to content

Instantly share code, notes, and snippets.

@s1moe2
Created March 21, 2021 18:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1moe2/e35cd0e59c17398eebf47faf361b3540 to your computer and use it in GitHub Desktop.
Save s1moe2/e35cd0e59c17398eebf47faf361b3540 to your computer and use it in GitHub Desktop.
Replace lines that start with a prefix in a file
func replaceImageInTemplate(filepath string, prefix string, newString string) error {
input, err := ioutil.ReadFile(filepath)
if err != nil {
return err
}
r, err := regexp.Compile(fmt.Sprintf("(?m)^%s(.*?)$", prefix))
if err != nil {
return err
}
newFile := r.ReplaceAll(input, []byte(newString))
if err = ioutil.WriteFile(filepath, newFile, 0666); err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment