使用Go语言的条件语句

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

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函数,或者使用stringsbytes包手动修剪行尾字符。

另外,你可以使用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")
}

huangapple
  • 本文由 发表于 2016年10月13日 22:23:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/40023725.html
匿名

发表评论

匿名网友

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

确定