Skip to content

Instantly share code, notes, and snippets.

@mattn
Forked from hyuki/a.go
Created April 9, 2018 08:56
Show Gist options
  • Save mattn/79258b545ca55c258c3cd970dc5f42cf to your computer and use it in GitHub Desktop.
Save mattn/79258b545ca55c258c3cd970dc5f42cf to your computer and use it in GitHub Desktop.
os.Create, fmt.Fprintf
package main
import (
"bufio"
"fmt"
"log"
"os"
)
func main() {
if len(os.Args) != 3 {
fmt.Printf("Usage: %s input-filename output-filename\n", os.Args[0])
os.Exit(1)
}
infilename := os.Args[1]
outfilename := os.Args[2]
fmt.Printf("input-filename = %s\n", infilename)
fmt.Printf("output-filename = %s\n", outfilename)
infile, err := os.Open(infilename)
if err != nil {
log.Fatal(err)
}
defer infile.Close()
outfile, err := os.Create(outfilename)
if err != nil {
log.Fatal(err)
}
defer outfile.Close()
linenumber := 0
scanner := bufio.NewScanner(infile)
for scanner.Scan() {
fmt.Fprintf(outfile, "%d: %s\n", linenumber, scanner.Text())
linenumber++
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading:", err)
}
}
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment