Golang的for循环不会停止。

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

Golang for loop wont stop

问题

所以我开始学习Go编程语言,希望有人能够解释一下为什么我得到这样的结果。我试图让程序从用户那里读取输入并将其显示回来,直到用户只输入换行符为止。

package main

import (
	"fmt"
	"os"
	"bufio"
	"strings"
)

func main(){

	inputBuff := bufio.NewReader(os.Stdin)
	line,_ := inputBuff.ReadString('\n')

	for  (!strings.EqualFold(line,"\n")){
		line,err := inputBuff.ReadString('\n')
		if err!=nil {
			fmt.Println(err)
		}
		fmt.Println(line)
		fmt.Println(!strings.EqualFold(line,"\n"))
	}

}

我试图一次读取完整的字符串,所以我认为使用bufio会比使用Scan更好。我添加了最后一个打印语句来显示该方法返回false,但循环仍然继续执行。任何帮助将不胜感激。

英文:

So I am starting to learn the Go Programming Language and was hoping someone might be able to clarify why I'm getting the results I'm getting. I'm trying to have the program read input from the user and display it back until the user only enters the newline character.
package main

import (
	"fmt"
	"os"
	"bufio"
	"strings"
)

func main(){

	inputBuff := bufio.NewReader(os.Stdin)
	line,_ := inputBuff.ReadString('\n')

	for  (!strings.EqualFold(line,"\n")){
		line,err := inputBuff.ReadString('\n')
		if err!=nil {
			fmt.Println(err)
		}
		fmt.Println(line)
		fmt.Println(!strings.EqualFold(line,"\n"))
	}

}

I am trying to read in full strings at a time so i thought the bufio would be better the using Scan. I added the last print to show that the method is returning false but the loop continues to execute. Any help would be greatly appreciated.

答案1

得分: 9

在循环内部使用的line与你为检查初始化的line不是同一个。修正后的代码如下:

package main

import (
    "fmt"
    "os"
    "bufio"
    "strings"
)

func main() {
    inputBuff := bufio.NewReader(os.Stdin)
    line, _ := inputBuff.ReadString('\n')
    var err error
    for !strings.EqualFold(line, "\n") {
        line, err = inputBuff.ReadString('\n')
        if err != nil {
            fmt.Println(err)
        }
        fmt.Println(line)
        fmt.Println(!strings.EqualFold(line, "\n"))
    }
}

你在循环内部使用了:=赋值运算符,例如line, err := ....。这会使Go在循环内部创建一个名为line的新符号。但是,for循环的检查是在循环代码块的内部作用域之外。因此,它引用了在循环外部初始化的旧line

将循环内部的运算符更改为=不会创建新变量,但也不会初始化err,所以我事先定义了它。err可以在循环内部声明,但这是多余的。

英文:

the line inside the loop is not the same line you initiated for the check. FTFY:

package main

import (
    "fmt"
    "os"
    "bufio"
    "strings"
)

func main(){

    inputBuff := bufio.NewReader(os.Stdin)
    line,_ := inputBuff.ReadString('\n')
    var err error
    for  (!strings.EqualFold(line,"\n")){
        line,err = inputBuff.ReadString('\n')
        if err!=nil {
            fmt.Println(err)
        }
        fmt.Println(line)
        fmt.Println(!strings.EqualFold(line,"\n"))
    }

You used the := assignment operator inside the loop, as in line, err := .....
This makes Go create a new symbol inside the the for loop with the name line. But the for's check is outside the inner scope of the loop code block. So it refers to the old line that was initiated outside the loop.

Changing the operator inside the loop to = doesn't create a new variable, but also doesn't init err, so I defined it beforehand too. err can be declared inside the loop but that's redundant.

huangapple
  • 本文由 发表于 2014年2月3日 15:17:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/21521709.html
匿名

发表评论

匿名网友

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

确定