Golang 如何获得用户的输入

在本教程中,我们将看到如何在 Golang 中获得用户的输入。Golang有一个由输入/输出函数组成的库,它有助于打印和获取输出。获取输入的函数是Scanln()。

算法

接受用户的整数输入

  • 第1步 – 我们正在导入有输入/输出函数的 fmt 包。
  • 第2步 – 我们正在声明一个变量,然后我们正在打印要求用户输入的行。

  • 第3步 – 然后使用fmt.Scanln(),我们将输入的内容存储到一个变量中。

  • 第4步 – 我们正在检查来自用户的输入,其模式为2或不为0,使用

例子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包。然后在主函数中,我们声明了一个字符串变量,之后我们打印了几行,要求用户输入。然后我们使用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.

这都是关于库和用来接受用户输入的函数。