-
-
Save hyuki/6517404a1ab368d2a3122e48d0334280 to your computer and use it in GitHub Desktop.
os.Create, fmt.Fprintf
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"log" | |
) | |
func check(err error) { | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
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) | |
check(err) | |
outfile, err := os.Create(outfilename) | |
check(err) | |
linenumber := 0 | |
scanner := bufio.NewScanner(infile) | |
for scanner.Scan() { | |
fmt.Fprintf(outfile, "%d: %s\n", linenumber, scanner.Text()) | |
linenumber++ | |
} | |
check(scanner.Err()) | |
check(infile.Close()) | |
check(outfile.Close()) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Author
hyuki
commented
Apr 9, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment