Golang Read Different Datatypes From Stdin Bufio

How practise i have input from User in Get Lang

Final updated Oct 22, 2021


In this Get example tutorial nosotros will comprehend accept user input in Go Lang. The user input can be added by using bufio bundle and fmt package.

Create an Input with bufio Package

This is the basic syntax to create an input in Get with bufio package.

              // create an input reader              var reader *bufio.Reader = bufio.NewReader(os.Stdin)              // get the input              var_name, err := reader.ReadString(delimiter)

In this example, the input is used in this program.

package main  import (              "bufio"                                            "fmt"                                            "os"              )  func main() {              // create an input reader                                          var reader *bufio.Reader = bufio.NewReader(os.Stdin)      fmt.Println("Enter your name: ")              // receive an input                                            // and then save it to the name variable                                          proper name, _ := reader.ReadString('              \n              ')      fmt.Println("Enter you lot age: ")              // receive an input                                            // then save it to the age variable                                          historic period, _ := reader.ReadString('              \n              ')      fmt.Println("Hello, ", proper name)     fmt.Println("Your historic period is: ", historic period) }

Output

            Enter your proper name: aiden jackson Enter yous age: 25 Howdy,  aiden jackson  Your age is:  25                      

Based on the code in a higher place, the reader is created then the input is received using ReadString('\n') method. The inputs are saved in proper name and age variable so the value from these variables is printed out.

Create an Input with fmt Package

This is the basic syntax to create an input with fmt package.

How do nosotros summate sum of two number from user input using Go Lang?

bundle chief  import (              "fmt"              )  func main() {              // create some variables to shop the inputs                                          var firstNum              int                                          var secondNum              int                                                        // receive some inputs                                          fmt.Println("Enter outset number: ")     fmt.Scanln(&firstNum)      fmt.Println("Enter second number: ")     fmt.Scanln(&secondNum)              // summate the sum of two numbers                                          var issue              int              = firstNum + secondNum              // print out the outcome                                          fmt.Println("The result: ", result) }

Output

            Enter first number: five Enter second number: 7 The result:  12                      

Based on the code above, the input is received with Scanln() method with variable name as the parameter.

How practice we summate boilerplate of numbers from user input using Get Lang?

packet main  import (              "fmt"              )  func main() {              // create some variables                                          var n              int                                          var data              int                                          fmt.Println("Average Calculator")     fmt.Println("Enter the amount of data: ")              // receive the amount of data                                          fmt.Scanln(&northward)      var nums []int              = []int{}              for              i :=              i; i <= n; i++ {         fmt.Println("Enter the number: ")              // receive the numbers                                          fmt.Scanln(&data)         nums = append(nums, data)     }              // calculate the boilerplate and then impress out the result                                          var averageResult              int              = average(nums)     fmt.Println("Boilerplate issue: ", averageResult) }              // create a part to summate the average              func boilerplate(nums []int)              int              {     var sum              int              =              0                                                        for              _, num := range nums {         sum += num     }      var issue              int              = sum / len(nums)              return              result }

Output

            Boilerplate Calculator Enter the corporeality of data: 5 Enter the number: 1 Enter the number: 2 Enter the number: 3 Enter the number: four Enter the number: 5 Average effect:  three                      

Based on the code above, the corporeality of information and data that will be calculated is received from the user input. And so the boilerplate calculation is performed.

I hope this commodity is helpful to learn how to create an user input in Go programming linguistic communication.


Article Contributed By :

https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

208 Views

Subscribe For Daily Updates

pearcemadmon.blogspot.com

Source: https://rrtutors.com/tutorials/User-Input-in-Go-with-Examples

0 Response to "Golang Read Different Datatypes From Stdin Bufio"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel