Hello, Golang community!
I’m new to Go and recently started reading “The Go Programming Language” by Donovan and Kernighan. In the first chapter (Chapter 1, Section 3) where they discuss finding duplicate lines in the standard input, I came across the following code:
func main() {
counts := make(map[string]int)
input := bufio.NewScanner(os.Stdin) // This line
for input.Scan() { // And this line
counts[input.Text()]++
}
for line, n := range counts {
if n > 1 {
fmt.Printf("%d\t%s\n", n, line)
}
}
}
As a Python developer with some knowledge of C, I’m having difficulty understanding the lines I’ve commented on. In languages like Python and C, obtaining user input is straightforward using functions like input()
and scanf()
, but Go seems to handle it differently. Can someone please help me understand how these lines work in Go?
Thank you for your assistance!