Go – 比较用户输入的字符串/字节切片

huangapple go评论88阅读模式
英文:

Go - Comparing strings/byte slices input by the user

问题

我正在从用户那里获取输入,但是当我尝试将其与字符串文字进行比较时,它不起作用。这只是一个测试而已。

我想设置这样一个机制,当输入一个空行(只是按下回车键)时,程序就退出。我不明白为什么字符串不能比较,因为当我打印它时,它们是相同的。

in := bufio.NewReader(os.Stdin);
input, err := in.ReadBytes('\n');

if err != nil {
fmt.Println("错误:", err)
}

if string(input) == "example" {
os.Exit(0)
}

英文:

I am getting input from the user, however when I try to compare it later on to a string literal it does not work. That is just a test though.

I would like to set it up so that when a blank line is entered (just hitting the enter/return key) the program exits. I don't understand why the strings are not comparing because when I print it, it comes out identical.

in := bufio.NewReader(os.Stdin); 
input, err := in.ReadBytes('\n');

if err != nil {
    fmt.Println("Error: ", err)
}
  
if string(input) == "example" {
    os.Exit(0)
}

答案1

得分: 4

string vs []byte

string 的定义:

string 是由所有8位字节组成的字符串,通常但不一定表示UTF-8编码的文本。字符串可以为空,但不能为nil。string类型的值是不可变的。

byte 的定义:

byte是uint8的别名,在所有方面都等同于uint8。它被约定用于区分字节值和8位无符号整数值。

这是什么意思?

  • []byte 是一个 byte 切片。切片可以为空。
  • string 元素是Unicode字符,可以由多个字节组成。
  • string 元素保留了数据的含义(编码),而 []byte 则没有。
  • string 类型定义了相等运算符,但 slice 类型没有。

正如你所看到的,它们是两种具有不同属性的不同类型。
有一篇很好的博文解释了不同的与字符串相关的类型[1]。

关于你代码片段中的问题。

请记住,in.ReadBytes(char) 返回一个包含 char 的字节切片。所以在你的代码中,input\n 结尾。如果你希望代码按预期工作,请尝试这样写:

if string(input) == "example\n" { // 或者在Windows上使用 "example\r\n"
    os.Exit(0)
}

还要确保你的终端代码页与你的 .go 源文件相同。要注意不同的行尾样式(Windows 使用 "\r\n"),标准的 Go 编译器在内部使用 utf8。

[1] Comparison of Go data types for string processing.

英文:

string vs []byte

string definition:

> string is the set of all strings of 8-bit bytes, conventionally but not necessarily representing UTF-8-encoded text. A string may be empty, but not nil. Values of string type are immutable.

byte definition:

> byte is an alias for uint8 and is equivalent to uint8 in all ways. It is used, by convention, to distinguish byte values from 8-bit unsigned integer values.

What does it mean?

  • []byte is a byte slice. slice can be empty.
  • string elements are unicode characters, which can have more then 1 byte.
  • string elements keep a meaning of data (encoding), []bytes not.
  • equality operator is defined for string type but not for slice type.

As you see they are two different types with different properties.
There is a great blog post explaining different string related types [1]

Regards the issue you have in your code snippet.

Bear in mind that in.ReadBytes(char) returns a byte slice with char inclusively. So in your code input ends with '\n'. If you want your code to work in desired way then try this:

if string(input) == "example\n" { // or "example\r\n" when on windows
    os.Exit(0)
}

Also make sure that your terminal code page is the same as your .go source file. Be aware about different end-line styles (Windows uses "\r\n"), Standard go compiler uses utf8 internally.

[1] Comparison of Go data types for string processing.

huangapple
  • 本文由 发表于 2013年9月24日 09:41:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/18971672.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定