Skip to content

Instantly share code, notes, and snippets.

@twobob
Last active January 28, 2026 01:03
Show Gist options
  • Select an option

  • Save twobob/45413b9ab4fa45aa5c49a30a37eb1782 to your computer and use it in GitHub Desktop.

Select an option

Save twobob/45413b9ab4fa45aa5c49a30a37eb1782 to your computer and use it in GitHub Desktop.
counts words passed from stdio in go, learning script
taskkill /F /IM counter.exe /T 2>$null
del /F counter.exe
go build -o counter.exe counter.go
echo yeah baby | counter.exe
2
// package main identifies this file as an executable program rather than a library.
package main
import (
"bufio" // Provides buffered I/O to read input efficiently.
"fmt" // Implements formatted I/O, used here for printing the result.
"os" // Provides a platform-independent interface to operating system functionality.
)
func main() {
// Initialize a new scanner that reads from Standard Input.
s := bufio.NewScanner(os.Stdin)
// Instruct the scanner to split input into words (whitespace-delimited).
// By default, a scanner splits by lines (\n).
s.Split(bufio.ScanWords)
// c acts as our word accumulator.
c := 0
// s.Scan() advances to the next word. It returns true if a token was found
// and false if the input is exhausted or an error occurred.
for s.Scan() {
c++
}
// Print the final count followed by a newline.
fmt.Println(c)
// Implementation Note: In a production environment, one should
// call s.Err() after the loop to check for read errors (e.g., token too long).
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment