如何在 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 的更多資訊,您可以瀏覽 此 教程。
廣告