英文:
If statements with Go
问题
我正在玩Go语言。我想要实现这样一个功能:当有人输入'hi'时,打印出'hiii'。
以下是我的代码:
package main
import (
"fmt"
"bufio"
"os"
)
func main(){
reader := bufio.NewReader(os.Stdin)
fmt.Println("Simple Shell")
fmt.Println("---------------------")
for {
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
if (text == "hi") {
fmt.Println("hiii")
}
}
}
请注意,这是一个简单的命令行程序,它会不断读取用户输入,并在用户输入'hi'时打印出'hiii'。
英文:
I am playing around with Go. I want to make it so when someone enters 'hi' it prints hiii
Here is my code
package main
import (
"fmt"
"bufio"
"os"
)
func main(){
reader := bufio.NewReader(os.Stdin)
fmt.Println("Simple Shell")
fmt.Println("---------------------")
for {
fmt.Print("-> ")
text, _ := reader.ReadString('\n')
if (text == "hi") {
fmt.Println("hiii")
}
}
}
答案1
得分: 3
有一个技巧可以解决这个问题:当使用带有分隔符的ReadString和ReadBytes函数时,返回的字符串(和字节)包含分隔符。这就是为什么条件不成立,你实际上的字符串是"hi\n"而不是"hi"。
为了从标准输入读取,你可以使用ReadLine函数,或者使用strings
和bytes
包手动修剪行尾字符。
另外,你可以使用Scanner,默认情况下它会读取行。以下是一些完成相同任务的示例代码:
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("-> ")
text, _, _ := reader.ReadLine()
if string(text) == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
stext, _ := reader.ReadString('\n') // stext以'\n'结尾,它读取了分隔符
stext = strings.TrimRight(stext, "\n")
if stext == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
text, _ = reader.ReadBytes('\n')
text = bytes.TrimRight(text, "\n")
if string(text) == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
stext = scanner.Text()
if stext == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
scanner.Scan()
text = scanner.Bytes()
if string(text) == "hi" {
fmt.Println("hii")
}
}
希望对你有帮助!
英文:
There is a trick to that: When using ReadString and ReadBytes function with a delimiter, the returned string (and bytes) contains the delimiter. That's why the condition is not true, your actual string is "hi\n" and not "hi".
In order to read from stdin, you can use the ReadLine function, or manually trim the end line characters with packages strings
and bytes
.
Also, you can use a Scanner, it reads lines by default. Here are some examples which all do the same job:
package main
import (
"bufio"
"bytes"
"fmt"
"os"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("-> ")
text, _, _ := reader.ReadLine()
if string(text) == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
stext, _ := reader.ReadString('\n') // stext ends with '\n', it reads the delimiter
stext = strings.TrimRight(stext, "\n")
if stext == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
text, _ = reader.ReadBytes('\n')
text = bytes.TrimRight(text, "\n")
if string(text) == "hi" {
fmt.Println("hii")
}
fmt.Print("-> ")
scanner := bufio.NewScanner(os.Stdin)
scanner.Scan()
stext = scanner.Text()
if stext == "hi" {
fmt.Println("hii")
}
fmt.Print("−> ")
scanner.Scan()
text = scanner.Bytes()
if string(text) == "hi" {
fmt.Println("hii")
}
}
答案2
得分: 0
只需添加\n
,如下所示:
if text == "hi\n" {
fmt.Println("hiii")
}
英文:
Just add \n
such that
if text == "hi\n" {
fmt.Println("hiii")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论