golang

Go 语言程序设计(4)

2019-03-31  本文已影响55人  hlemon

americanise.go 示例代码:

package main

import (
    "bufio"
    "fmt"
    "io"
    "io/ioutil"
    "log"
    "os"
    "path/filepath"
    "regexp"
    "strings"
)

var britishAmerican = "./british-american.txt"

func main() {
    inFilename, outFilename, err := filenameFromCommandLine()
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
    inFile, outFile := os.Stdin, os.Stdout
    if inFilename != "" {
        if inFile, err = os.Open(inFilename); err != nil {
            log.Fatal(err)
        }
        defer inFile.Close()
    }
    if outFilename != "" {
        if outFile, err = os.Create(outFilename); err != nil {
            log.Fatal(err)
        }
        defer outFile.Close()
    }
    if err = americanise(inFile, outFile); err != nil {
        log.Fatal(err)
    }
}

func filenameFromCommandLine() (inFilename, outFilename string, err error) {
    if len(os.Args) > 1 && (os.Args[1] == "-h" || os.Args[1] == "--help") {
        err = fmt.Errorf("usage: %s [<]infile.txt [>]outfile.txt", filepath.Base(os.Args[0]))
        return "", "", err
    }
    if len(os.Args) >1 {
        inFilename = os.Args[1]
        if len(os.Args) > 2 {
            outFilename = os.Args[2]
        }
    }
    if inFilename != "" && inFilename == outFilename {
        log.Fatal("won't overwrite the infile")
    }
    return inFilename, outFilename, nil
}

func americanise(inFile io.Reader, outFile io.Writer) (err error)  {
    reader := bufio.NewReader(inFile)
    writer := bufio.NewWriter(outFile)
    defer func() {
        if err == nil {
            err = writer.Flush()
        }
    }()
    var replacer func(string) string
    britishAmericanPath, _ := filepath.Abs(britishAmerican)
    if replacer, err = makeReplacerFunc(britishAmericanPath); err != nil {
        return err
    }
    wordRx := regexp.MustCompile("[A-Za-z]+")
    eof := false
    for !eof {
        var line string
        line, err = reader.ReadString('\n')
        if err == io.EOF {
            err = nil
            eof = true
        } else if err != nil {
            return err
        }
        line = wordRx.ReplaceAllStringFunc(line, replacer)
        if _, err = writer.WriteString(line); err != nil {
            return err
        }
    }
    return nil
}

func makeReplacerFunc(file string) (func(string) string, error) {
    rawBytes, err := ioutil.ReadFile(file)
    if err != nil {
        return nil, err
    }
    text := string(rawBytes)

    usForBritish := make(map[string] string)
    lines := strings.Split(text, "\n")
    for _, line := range lines {
        fields := strings.Fields(line)
        if len(fields) == 2 {
            usForBritish[fields[0]] = fields[1]
        }
    }

    return func(word string) string {
        if usWord, found := usForBritish[word]; found {
            return usWord
        }
        return word
    }, nil
}

知识点:

/* 声明变量,默认 map 是 nil */
var map_variable map[key_data_type]value_data_type

/* 使用 make 函数 */
map_variable := make(map[key_data_type]value_data_type)

defer 用法:

通过使用 defer 修饰一个函数,使其在外部函数 "返回后" 才被执行,即便外部的函数返回的是 panic 异常,这类函数被称作 延迟调用函数
注意点:

常见使用场景:

相关文档:

上一篇下一篇

猜你喜欢

热点阅读