如何在 Golang 中获取用户输入?
在本教程中,我们将了解如何在 Golang 中获取用户输入。Golang 有一个包含输入/输出函数的库,有助于打印和获取输出。获取输入的函数是 Scanln()。
算法
获取用户输入的整数
步骤 1 − 我们正在导入包含输入/输出函数的 fmt 包。
步骤 2 − 我们声明一个变量,然后打印提示用户输入的行。
步骤 3 − 然后使用 fmt.Scanln() 获取输入并将其存储到变量中。
步骤 4 − 我们使用以下方法检查用户的输入是否能被 2 整除:
示例 1
在此示例中,我们将从用户那里获取整数输入。
package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the variable using the var keyword var numberFromUser int fmt.Println("Please enter the number you want to check that it is divisible by 2 or not:") // scanning the input by the user fmt.Scanln(&numberFromUser) // logic to check that number is divisible by 2 or not if numberFromUser%2 == 0 { fmt.Println(numberFromUser, "is divisible by 2") } else { fmt.Println(numberFromUser, "is not divisible by 2") } }
输出 1
Please enter the number you want to check that it is divisible by 2 or not: 33 33 is not divisible by 2
输出 2
Please enter the number you want to check that it is divisible by 2 or not: 28 28 is divisible by 2
示例 2
在此示例中,我们将从用户那里获取字符串输入。
package main // fmt package provides the function to print anything import "fmt" func main() { // declaring the float variables var number1, number2, answer float32 fmt.Println("Enter the numbers on which you want to perform arithmetic operators.") // scanning the input by the user fmt.Println("Enter the first number.") fmt.Scanln(&number1) fmt.Println("Enter the second number.") fmt.Scanln(&number2) // declaring the string variable using the var keyword var operator string fmt.Println("Enter the operator you want to perform on two numbers.") // scanning the input by the user fmt.Scanln(&operator) switch operator { case "+": answer = number1 + number2 fmt.Println("The addition of", number1, "and", number2, "is", answer) case "-": answer = number1 - number2 fmt.Println("The subtraction of", number1, "and", number2, "is", answer) case "*": answer = number1 * number2 fmt.Println("The multiplication of", number1, "and", number2, "is", answer) case "/": answer = number1 / number2 fmt.Println("The division of", number1, "and", number2, "is", answer) } }
在上面的代码中,我们正在执行算术运算符,其中
首先,我们声明三个浮点型变量,其中两个存储用户输入的数字,第三个将在执行算术运算后存储答案。
现在我们从用户那里获取算术运算符作为输入。
最后,我们使用 switch case 来比较运算符输入并执行和打印相应的答案。
输出 1
Enter the numbers on which you want to perform arithmetic operators. Enter the first number. 10 Enter the second number. 20 Enter the operator you want to perform on two numbers. + The addition of 10 and 20 is 30.
输出 2
Enter the numbers on which you want to perform arithmetic operators. Enter the first number. 30 Enter the second number. 10 Enter the operator you want to perform on two numbers. - The subtraction of 30 and 10 is 20.
输出 3
Enter the numbers on which you want to perform arithmetic operators. Enter the first number. 30 Enter the second number. 5 Enter the operator you want to perform on two numbers. * The multiplication of 30 and 5 is 150.
输出 4
Enter the numbers on which you want to perform arithmetic operators. Enter the first number. 10 Enter the second number. 2 Enter the operator you want to perform on two numbers. / The division of 10 and 2 is 5.
示例 3
获取两个字符串单词作为输入。
package main import ( "bufio" "fmt" "os" "strings" ) func main() { // declaring the variable using the var keyword var capitalOfSouthAfrica string fmt.Println("What is the capital of South Africa") fmt.Println("Please enter your answer:") // scanning the input by the user inputReader := bufio.NewReader(os.Stdin) capitalOfSouthAfrica, _ = inputReader.ReadString('\n') // remove the delimiter from the string capitalOfSouthAfrica = strings.TrimSuffix(capitalOfSouthAfrica, "\n") if capitalOfSouthAfrica == "Capetown" { fmt.Println("You are right!") } else { fmt.Println("Sorry,the wrong answer, it is not", capitalOfSouthAfrica, ", it's Capetown.") } }
在上面的示例中,首先,我们导入包含输入/输出函数的 bufio 包。然后在 main 函数内部,我们声明一个字符串变量,然后打印提示用户输入的行。然后使用 bufio 创建一个输入读取器对象,它在下一行读取字符串输入并将其存储到变量中。最后,我们使用 if 条件将用户的输入与 Capetown 进行比较,并根据用户是否提供了正确的输入进行相应打印。
输出 1
What is the capital of South Africa Please enter your answer: Capetown You are right!
输出 2
What is the capital of South Africa Please enter your answer: New York Sorry, the wrong answer, it is not New York, it's Capetown.
这就是关于用于获取用户输入的库和函数的所有内容。要了解有关 Golang 的更多信息,您可以浏览 此 教程。
广告