Re: [go-nuts] weird thing happened to string concatenation on win7 OS

85 views
Skip to first unread message

Tad Glines

unread,
May 16, 2013, 7:45:28 PM5/16/13
to 卢红, golang-nuts
reader.ReadString('\n') is splitting the input on '\n', but on Windows the line delimiter is "\r\n" so the "\r|" remains at the end of the line returned by ReadString('\n'). You need to either switch to using ReadLine() or check for and trim off the "\r" at the end of the line.



On Thu, May 16, 2013 at 4:08 PM, 卢红 <cma...@gmail.com> wrote:
package main

import (
"fmt"
"bufio"
"io"
"os"
"regexp"
)

func main() {
configFile, configErr := os.Open(".\\config.ini")
properties := make(map[string]string)
reader := bufio.NewReader(configFile)
matcher, _ := regexp.Compile("(^\\w+)(\\s*=\\s*)(.*)")
var content string
var err error
for {
content, err = reader.ReadString('\n')
kv := matcher.FindAllStringSubmatch(content, 1)
if len(kv) > 0 {
properties[kv[0][1]] = kv[0][3]
}
if io.EOF == err {
break
}
}
if nil != configErr {
fmt.Println("Failed to configure program -> ", configErr)
fmt.Scanln()
return
}
localMp3Path := properties["saveLocation"]
fmt.Println(localMp3Path + "hello worlddddddddddddddddddddddddddd")
}


the content of config.ini
saveLocation=c:\golang\workspace\helloworld\test\src\

the expected result:
c:\golang\workspace\helloworld\test\src\hello worlddddddddddddddddddddddddddd

but the actual output result:
hello worlddddddddddhelloworld\test\src\

what's happened to the string from regexp function...

Thanks

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

卢红

unread,
May 16, 2013, 8:37:50 PM5/16/13
to golan...@googlegroups.com, 卢红
thanks very much. change to ReadLine(). the problem is solved.

dlin

unread,
May 16, 2013, 8:49:05 PM5/16/13
to golan...@googlegroups.com, 卢红
After Go1.1, use this to read line

http://golang.org/pkg/bufio/#example_Scanner_lines
Reply all
Reply to author
Forward
0 new messages