Skip to content

Instantly share code, notes, and snippets.

@lcaballero
Created September 17, 2015 21:02
Show Gist options
  • Save lcaballero/4b7b65565e7842822d10 to your computer and use it in GitHub Desktop.
Save lcaballero/4b7b65565e7842822d10 to your computer and use it in GitHub Desktop.
Golang line reading that indents.
func IndentLines(indent, code string) string {
reader := bufio.NewReader(bytes.NewBufferString(code))
buf := bytes.NewBufferString("")
line, prefix, err := reader.ReadLine()
for err == nil && line != nil {
if !prefix {
buf.WriteString(indent)
}
s := strings.Trim(string(line), " \t\r\n")
buf.WriteString(s)
if !prefix {
buf.WriteString("\n")
}
line, prefix, err = reader.ReadLine()
}
return buf.String()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment