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的信息,您可以探索这里的教程。