Created
September 17, 2015 21:02
-
-
Save lcaballero/4b7b65565e7842822d10 to your computer and use it in GitHub Desktop.
Golang line reading that indents.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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