simpleInput is a Go package that provides a simple and efficient way to prompt users for input in the command line. With simpleInput, developers can save time and effort by eliminating the need to deal with pointers or addresses, and by allowing them to input multiple values in a single line of code. The package includes a single function, input, which takes one or more string arguments as prompts and returns a slice of strings containing the user's input values. The function uses the bufio package to read input values from the standard input, and then returns the input values as a slice. This code is a great starting point for developers who want to quickly and easily prompt users for input in their Go projects, without having to write multiple lines of code for each message or input assignment.
To use simpleInput in your Go project, run the following command:
go get github.com/Hcode00/simpleInput
To use simpleInput, import it in your Go code:
import s "github.com/Hcode00/simpleInput"
To get a single input value, call the input function with a single prompt and retrieve the first element of the resulting slice:
name := s.Input("Enter your name: ")[0] // John
fmt.Printf("Hello %s.\n", name) // Hello John
To get multiple input values, call the input function with multiple prompts and retrieve the element
inputs := s.Input("Enter your name: ", "Enter your age: ", "Enter your gender: ") // John 21 Male
name, age, gender := inputs[0], inputs[1], inputs[2] // name = "John" , age = "21" , gender = "Male"
fmt.Printf("Hello %s, you are %s years old and you are a %s.\n", name, age, gender) //Hello John, you are 21 years old and you are a Male.\n
To get input values in a loop, call the input function with a prompt inside the loop and append the resulting values to a slice:
var inputs []string
for i := 0; i < 3; i++ {
inputs = append(inputs, simpleInput.input(fmt.Sprintf("Enter value %d: ", i+1))[0])
// Enter value 1: 12
// Enter value 2: 25
// Enter value 3: "Hello World"
}
fmt.Printf("You entered: %v\n", inputs) // You entered: [12 25 Hello World]