跳过功能

时间:2018-09-23 19:08:40

标签: go

我正在尝试运行此代码,但是他跳过了函数登录

package main

import "fmt"

var name string
var password string

func getName() {
    fmt.Print("What's your name : \n")
    fmt.Scanf("%s", &name)
}

func showName() {
    fmt.Print("Your name is " + name + "\n")
}

func getPassword() {
    fmt.Print("What's your password : \n")
    fmt.Scanf("%s \n", &password)
}

func singOrLog() {
    var input string
    input = ""
    var loop bool
    loop = false

    for loop == false {
        fmt.Print("Do you want to make an account or login (0 for register and 1 login) : ")
        fmt.Scanf("%s \n", &input)

        if input == "0" {
            register()
            loop = true
        } else if input == "1" {
            login()
            loop = true
        }
    }
}

func login() {
    var nameCorrect string
    nameCorrect = name
    for !(nameCorrect == name) {
        fmt.Print("What's your name :\n")
        fmt.Scanf("%s", &nameCorrect)
    }
}

func register() {
    getName()
    showName()
    getPassword()
    login()
}

func main() {
    singOrLog()
}

1 个答案:

答案 0 :(得分:2)

虽然不会打印任何内容,但不会跳过该登录功能。该行:

bootstrap.log

将nameCorrect设置为与name相同,后跟:

nameCorrect = name

仅当name和nameCorrect不同时才执行。您可以通过添加以下行来看到此内容:

for !(nameCorrect == name) {

将打印:

fmt.Printf("Name: '%s', nameCorrect: '%s', !(nameCorrect == name): %t\n", name, nameCorrect, !(nameCorrect == name))

我认为您不希望有Name: '', nameCorrect: '', !(nameCorrect == name): false 行。