laurent,
What specifically don't you like?
Here's a more idiomatic version your program with test results.
package main
import (
"bytes"
"fmt"
"io"
)
type DirectoryInfoWriter struct {
writer io.Writer
}
// this function escape '\n' '\r' ';' ',' character
// with the '\\' character
func (di *DirectoryInfoWriter) WriteValue(value string) {
i := 0
for _, c := range value {
if i == 76 {
// if line to long fold value on multiple line
io.WriteString(di.writer, "\n ")
i = 0
}
var e string
switch c {
case '\r':
e = `\r`
case '\n':
e = `\n`
case ';':
e = `\;`
case ',':
e = `\,`
default:
// c is an int representing a Unicode code point.
// convert it to string (UTF-8 encoded character)
e = string(c)
}
io.WriteString(di.writer, e)
i++
}
}
func main() {
w := bytes.NewBufferString("")
di := DirectoryInfoWriter{w}
v := "| \n | \r | ; | , |"
fmt.Println(len(v), []byte(v))
di.WriteValue(v)
s := w.String()
fmt.Println(len(s), []byte(s))
fmt.Println(len(s), s)
}
Output:
17 [124 32 10 32 124 32 13 32 124 32 59 32 124 32 44 32 124]
21 [124 32 92 110 32 124 32 92 114 32 124 32 92 59 32 124 32 92 44 32
124]
21 | \n | \r | \; | \, |
Peter