Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

strings: Can't check strings.HasSuffix on string captured with reader.ReadString #17669

Closed
jupmorenor opened this issue Oct 30, 2016 · 5 comments

Comments

@jupmorenor
Copy link

go version go1.7
windows7/386

What did you do?

I was capturing strings with fmt.Scanln, but one of my needs is that the string must have spaces so I started to use reader.ReadString, I have to check the presence of a suffix in that string to make some replacing, so I used strings.HasSuffix. With strings captured with fmt.Scanln there was no problema but now that I use reader.ReadString it is unabled to find the given suffix in the string.

package main

import (
    "bufio"
    "fmt"
    "strings"
    "os"
    "reflect"
)
const TOKEN string = ":="

func validate(expr string) {
    fmt.Println("var type: ", reflect.TypeOf(expr))
    if strings.Contains(expr, TOKEN) {
        fmt.Println(expr, "contains works")
    } else {
        fmt.Println("error with contains")
    }

    if strings.HasSuffix(expr, TOKEN) {
        fmt.Println(expr, "suffix works")
    } else {
        fmt.Println("error with suffix")
    }
}

func main() {
    var expr2 string
    reader := bufio.NewReader(os.Stdin)
    fmt.Print("type something finished with ':='")
    expr1, _ := reader.ReadString('\n')
    fmt.Scanln(&expr2)

    fmt.Println("validation with reader: ")
    validate(expr1)
    fmt.Println()
    fmt.Println("validation with Scanln: ")
    validate(expr2)
}

What did you expect to see?

It is supposed to work, as both are the same type (string).

What did you see instead?

The check of suffix on strings captured with reader always fails.

@bradfitz
Copy link
Contributor

For help with Go, see https://golang.org/wiki/Questions

@mikioh mikioh changed the title Can't check strings.HasSuffix on string captured with reader.ReadString strings: Can't check strings.HasSuffix on string captured with reader.ReadString Oct 30, 2016
@jupmorenor
Copy link
Author

Couldn't it be uniform for any text capture method?

@dominikh
Copy link
Member

This isn't an issue with Go, but with your code. Your code has a bug. Please see https://golang.org/wiki/Questions to find the right venue to ask for help. The issue tracker is for tracking bugs and features, not providing help. Thanks.

@jupmorenor
Copy link
Author

I expected that any string could be treated as the same, being indifferent the way it was captured, that's the reason I posted it as an issue.
Anyway I'm asking for help everywhere.

@bradfitz
Copy link
Contributor

I expected that any string could be treated as the same, being indifferent the way it was captured

It can. But your string is not what you think it is.

@golang golang locked and limited conversation to collaborators Oct 30, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants