-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03_input_from_the_console.go
57 lines (46 loc) · 1.64 KB
/
03_input_from_the_console.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
//testScanf()
//testBufioOs()
//testBufioOsNumericValues()
}
// Scan methods are designed to get input from console, files, variables, and
// other input sources and automatically separate values from each other by
// looking for space characters
func testScanf() {
var s string
// Scan function is designed for analyzing and parsing the string
// It automatically breaks up the string whenever it finds space characters
fmt.Scanln(&s) // --> & character passes the reference of s
fmt.Println(s)
}
// If you want simply to collect user input in a console application, you should
// instead use a couple of packages bufio and os
func testBufioOs() {
// a reader object can collect information from a variety of inputs
reader := bufio.NewReader(os.Stdin) // --> this means that this reader object is looking for information from standard input
fmt.Print("Enter text: ")
str, _ := reader.ReadString('\n') // --> the character defines when the ReadString operation needs to return
// --> in this case, it will return whatever it reads until the new line character
fmt.Println(str)
}
func testBufioOsNumericValues() {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter a number: ")
str, _ := reader.ReadString('\n')
// strconv is used for converting string values to different types
f, err := strconv.ParseFloat(strings.TrimSpace(str), 64) // 64 indicates the bitsize of the float number going to be returned
// TrimSpace will remove the leading or trailing spaces from the string
if err != nil {
fmt.Println(err)
} else {
fmt.Println("Value of number:", f)
}
}