One, word substitution
1 Package Main2 3 Import (4 "Bufio"5 "FMT"6 "io"7 "Io/ioutil"8 "Log"9 "OS"Ten "Path/filepath" One "RegExp" A "Strings" - ) - the varBritishamerican ="British-american.txt" - - func init () { -Dir, _: = FilePath. Split (OS. args[0]) +Britishamerican =filepath. Join (dir, Britishamerican) -Fmt. Println ("dir%s,file%s", Dir,britishamerican) + } A at Func Main () { -Infilename, Outfilename, err: =Filenamesfromcommandline () - ifErr! =Nil { - FMT. PRINTLN (ERR) -Os. Exit (1) - } inInFile, OutFile: =OS. Stdin, OS. Stdout - ifInfilename! ="" { to ifInFile, err = os. Open (Infilename); Err! =Nil { + log. Fatal (ERR) - } the defer infile.close () * } $ ifOutfilename! ="" {Panax Notoginseng ifOutFile, err = os. Create (Outfilename); Err! =Nil { - log. Fatal (ERR) the } + defer outfile.close () A } the + ifErr = Americanise (InFile, outFile); Err! =Nil { - log. Fatal (ERR) $ } $ } - -Func filenamesfromcommandline () (Infilename, Outfilenamestring, err Error) { the ifLen (OS. Args) >1&& (OS. args[1] =="- H"|| Os. args[1] =="--help") { -Err = FMT. Errorf ("usage:%s [<] infile.txt [;] Outfile.txt",WuyiFilePath. Base (OS. args[0])) the return "","", Err - } Wu ifLen (OS. Args) >1 { -Infilename = OS. args[1] About ifLen (OS. Args) >2 { $Outfilename = OS. args[2] - } - } - ifInfilename! =""&& Infilename = =Outfilename { ALog. Fatal ("won ' t overwrite the infile") + } the returnInfilename, outfilename, nil - } $ the func americanise (inFile io. Reader, OutFile io. Writer) (err error) { theReader: =Bufio. Newreader (InFile) theWriter: =Bufio. Newwriter (OutFile) the defer func () { - ifErr = =Nil { inErr =writer. Flush () the } the }() About the varReplacer func (string)string the ifReplacer, err = makereplacerfunction (Britishamerican); Err! =Nil { the returnErr + } -Wordrx: = RegExp. Mustcompile ("[a-za-z]+") theEOF: =falseBayi for!EOF { the varLinestring theLine, err = reader. ReadString ('\ n') - ifErr = =io. EOF { -Err =Nil theEOF =true the}Else ifErr! =Nil { the returnErr the } -line =Wordrx.replaceallstringfunc (line, replacer) the if_, Err = writer. WriteString (line); Err! =Nil { the the returnErr94 } the } the returnNil the }98 AboutFunc makereplacerfunction (filestring) (Func (string)string, error) { -Rawbytes, err: =Ioutil. ReadFile (file)101 ifErr! =Nil {102 returnNil, err103 }104Text: =string(rawbytes) theUsforbritish: = Make (map[string]string)106Lines: = Strings. Split (Text,"\ n")107 for_, Line: =Range Lines {108Fields: =strings. Fields (line)109 ifLen (fields) = =2 { theusforbritish[fields[0]] = fields[1]111 } the }113 the returnFunc (Wordstring)string { the ifUsword, found: =Usforbritish[word]; found { the returnUsword117 }118 returnWord119 }, Nil -}
The format is finally right.
In this function, I think there are a few points, one is closure, one is a regular replacement, there is a defer to do the funeral ~
A simple understanding of closures, when constructing a function, while allowing the function to bring some other content such as data, such as the definition of this replacer function, the first definition of the Replacer private data map, and then return the use of this map function. This allows you to use this map point data when calling Replacer. (It seems to be a bit confusing.) )
The regular and the defer have nothing to say.
"Go Language Programming" study (III)