We can only guess. You haven't defined your problem clearly. Please clarify your specific problem or add additional details to highlight exactly what you need. Provide sample input and output.
The Go Programming Language Specification
After a backslash, certain single-character escapes represent special
values:
U+000A line feed or newline
Here's a guess:
lines.go
:
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
func lines(data []byte) {
text := string(data)
for i, j := 0, 0; j >= 0; i += j + 1 {
var line string
j = strings.IndexByte(text[i:], '
')
if j < 0 {
line = text[i:]
if len(line) == 0 {
continue
}
} else {
line = text[i : i+j+1]
}
// process line
fmt.Printf("%d %q
", len(line), line)
}
}
func main() {
filename := `test.file`
data, err := ioutil.ReadFile(filename)
if err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
fmt.Printf("%d %q
", len(data), data)
lines(data)
}
Output:
$ cat test.file
line1
line2
line3
$ go run lines.go
18 "line1
line2
line3
"
6 "line1
"
6 "line2
"
6 "line3
"
Comment:
I'll try and clarify it through an example. Let say that I have a file
with contents "ara/n;>$g9s", my application will perform an action
defined by that input character as it moves through the contents of
the file. I.e. If "a" does action 1, "r" does action 2, "/n" does
action 3 and so on then the input above will perform the following
actions 1,2,1,3... in that order. However, if you turn the byte array
to a string then I'm unable to identify "/n" characters since they
appear to be removed despite the string having the same formatting as
before if you print it out or concat it into a file. – Elliot Smith
Why do you write /n
for a newline character! The newline character U+000A, as I've already pointed out, is written as
.
For example,
package main
import "fmt"
func es(s string) {
for _, r := range s {
switch r {
case 'a':
fmt.Printf("action 1 for %q
", r)
case 'r':
fmt.Printf("action 2 for %q
", r)
case '
':
fmt.Printf("action 3 for %q
", r)
default:
fmt.Printf("action ? for %q
", r)
}
}
}
func main() {
b := []byte("ara
;>$g9s")
s := string(b)
es(s)
}
Playground: https://play.golang.org/p/3J0pxXh3Wkc
Output:
action 1 for
action 2 for
action 1 for
action 3 for
action ? for
action ? for
action ? for
action ? for
action ? for
action ? for
Revised Question:
Example: If the file contents is "r/n3$/n;" then I should able to
perform 6 predefined actions (one for for each character) as I move
from left to right over the files contents. Elliot Smith
Why do you write /n
for a newline character! The newline character U+000A, as I've already pointed out, is written as
.
For example,
package main
import "fmt"
func es(s string) {
for _, r := range s {
switch r {
case 'a':
fmt.Printf("action for %q
", r)
case 'r':
fmt.Printf("action for %q
", r)
case '
':
fmt.Printf("action for %q
", r)
default:
fmt.Printf("action for %q
", r)
}
}
}
func main() {
file := []byte("r
3$
;")
s := string(file)
es(s)
}
Playground: https://play.golang.org/p/X1gtrPRmlqq
Output:
action for
action for
action for
action for
action for
action for