FAQ
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\
baseUrl=http://test.com/eng/ez/morning

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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Tad Glines at May 16, 2013 at 11:45 pm
    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, 卢红 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\
    baseUrl=http://test.com/eng/ez/morning

    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • 卢红 at May 17, 2013 at 12:37 am
    thanks very much. change to ReadLine(). the problem is solved.
    On Thursday, May 16, 2013 7:45:28 PM UTC-4, Tad Glines wrote:

    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 <javascript:>>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\
    baseUrl=http://test.com/eng/ez/morning

    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 <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Dlin at May 17, 2013 at 12:49 am
    After Go1.1, use this to read line

    http://golang.org/pkg/bufio/#example_Scanner_lines
    On Friday, May 17, 2013 8:37:50 AM UTC+8, 卢红 wrote:

    thanks very much. change to ReadLine(). the problem is solved.
    On Thursday, May 16, 2013 7:45:28 PM UTC-4, Tad Glines wrote:

    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, 卢红 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\
    baseUrl=http://test.com/eng/ez/morning

    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.

    --
    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+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 16, '13 at 11:35p
activeMay 17, '13 at 12:49a
posts4
users3
websitegolang.org

3 users in discussion

卢红: 2 posts Tad Glines: 1 post Dlin: 1 post

People

Translate

site design / logo © 2021 Grokbase