如何使用Golang自定义扫描程序字符串文字并扩展内存以将整个文件加载到内存中?

时间:2017-10-06 16:07:24

标签: go string-literals

我一直试图弄清楚如何实现我原先认为的简单程序。 我有一个引号的文本文件,全部用'$$'

分隔

我希望程序解析引用文件并随机选择3个引号来显示和标准输出。

文件中有1022个引号。

当我尝试拆分文件时出现此错误:  缺少'

我似乎无法弄清楚如何使用字符串文字指定$$,我一直得到:
    缺少'

这是自定义扫描程序:

onDollarSign := func(data []byte, atEOF bool) (advance int, token []byte, err error) {  
    for i := 0; i < len(data); i++ { 
        //if data[i] == "$$" {              # this is what I did originally
        //if data[i:i+2] == "$$" {    # (mismatched types []byte and string)
        //if data[i:i+2] == `$$` {    # throws (mismatched types []byte and string)
        // below throws syntax error: unexpected $ AND missing '
        if data[1:i+2] == '$$' {   
            return i + 1, data[:i], nil  
        }  
    }  

如果我只使用一个$,则字符串文字可以正常工作。

出于某种原因只有71个引号加载到引号切片中。我不确定如何扩展。允许所有1022引号存储在内存中。

我一直很难找到如何做到这一点。这就是我现在所拥有的:

package main
import (  
    "bufio"  
    "fmt"  
    "log"  
    "math/rand"  
    "os"  
    "time"  
)  

func main() {  
    rand.Seed(time.Now().UnixNano()) // Try changing this number!  
    quote_file, err := os.Open("/Users/bryan/Dropbox/quotes_file.txt")  
    if err != nil {  
        log.Fatal(err)  
    }  
    scanner := bufio.NewScanner(quote_file)  
    // define split function  
    onDollarSign := func(data []byte, atEOF bool) (advance int, token []byte, err error) {  
        for i := 0; i < len(data); i++ {  
            if data[i] == '$$' {  
                return i + 1, data[:i], nil  
            }  
        }  
        fmt.Print(data)  
        return 0, data, bufio.ErrFinalToken  
    }  
    scanner.Split(onDollarSign)  
    var quotes []string  

    // I think this will scan the file and append all the parsed quotes into quotes  
    for scanner.Scan() {  
        quotes = append(quotes, scanner.Text())  

    }  
    if err := scanner.Err(); err != nil {  
        fmt.Fprintln(os.Stderr, "reading input:", err)  
    }  
    fmt.Print(len(quotes))  
    fmt.Println("quote 1:", quotes[rand.Intn(len(quotes))])  
    fmt.Println("quote 2:", quotes[rand.Intn(len(quotes))])  
    fmt.Println("quote 3:", quotes[rand.Intn(len(quotes))])  
}  

4 个答案:

答案 0 :(得分:3)

在golang单引号中'用于单个字符(所谓的&#34;符文&#34; - 内部它是带有unicode代码点的int32)和字符串的双引号可以超过1个字符:"$$"

所以解析器在第一个美元符号后面等待一个结束符文联盟'

这是一篇好文章:https://blog.golang.org/strings

更新:如果您想避免将所有data转换为字符串,可以选择以下方式:

...
   onDollarSign := func(data []byte, atEOF bool) (advance int, token []byte, err error) {  
        for i := 0; i < len(data); i++ {  
            if data[i] == '$' && data[i+1] == '$' {  /////   <----
                return i + 1, data[:i], nil  
            }  
        }  
        fmt.Print(data)  
        return 0, data, bufio.ErrFinalToken  
    } 
...

答案 1 :(得分:3)

如果您最终阅读整个文件,请使用扫描仪,这有点令人费解。我读完整个文件,然后将其拆分为引号列表:

package main

import (
    "bytes"
    "io/ioutil"
    "log"
    "math/rand"
    "os"
)

func main() {
    // Slurp file.
    contents, err := ioutil.ReadFile("/Users/bryan/Dropbox/quotes_file.txt")
    if err != nil {
            log.Fatal(err)
    }

    // Split the quotes
    separator := []byte("$$") // Convert string to []byte
    quotes := bytes.Split(contents, separator)

    // Select three random quotes and write them to stdout
    for i := 0; i < 3; i++ {
            n := rand.Intn(len(quotes))
            quote := quotes[n]

            os.Stdout.Write(quote)
            os.Stdout.Write([]byte{'\n'}) // new line, if necessary
    }
}

如果在读取文件之前选择了三个引号,则使用扫描仪是有意义的;然后你可以在到达最后一个引用后停止阅读。

答案 2 :(得分:1)

我根据stdlib函数bufio.Scanlines重新编写了split函数。

我还没有彻底测试过,所以你应该锻炼它。您还应该决定如何处理空格,例如文件末尾的换行符。

func onDollarSign(data []byte, atEOF bool) (advance int, token []byte, err error) {

    // If we are at the end of the file and there's no more data then we're done
    if atEOF && len(data) == 0 {
        return 0, nil, nil
    }

    // If we are at the end of the file and there IS more data return it
    if atEOF {
        return len(data), data, nil
    }

    // If we find a $ then check if the next rune after is also a $. If so we
    // want to advance past the second $ and return a token up to but not
    // including the first $.
    if i := bytes.IndexByte(data, '$'); i >= 0 {
        if len(data) > i && data[i+1] == '$' {
            return i + 2, data[0:i], nil
        }
    }

    // Request more data.
    return 0, nil, nil
}

答案 3 :(得分:1)

扫描引号(scanQuotes)类似于扫描行(bufio.ScanLines)。例如,

package main

import (
    "bufio"
    "bytes"
    "fmt"
    "os"
    "strings"
)

func dropCRLF(data []byte) []byte {
    if len(data) > 0 && data[len(data)-1] == '\n' {
        data = data[0 : len(data)-1]
        if len(data) > 0 && data[len(data)-1] == '\r' {
            data = data[0 : len(data)-1]
        }
    }
    return data
}

func scanQuotes(data []byte, atEOF bool) (advance int, token []byte, err error) {
    if atEOF && len(dropCRLF(data)) == 0 {
        return len(data), nil, nil
    }
    sep := []byte("$$")
    if i := bytes.Index(data, sep); i >= 0 {
        return i + len(sep), dropCRLF(data[0:i]), nil
    }
    if atEOF {
        return len(data), dropCRLF(data), nil
    }
    return 0, nil, nil
}

func main() {
    /*
       quote_file, err := os.Open("/Users/bryan/Dropbox/quotes_file.txt")
       if err != nil {
        log.Fatal(err)
       }
    */
    quote_file := strings.NewReader(shakespeare) // test data

    var quotes []string
    scanner := bufio.NewScanner(quote_file)
    scanner.Split(scanQuotes)
    for scanner.Scan() {
        quotes = append(quotes, scanner.Text())
    }
    if err := scanner.Err(); err != nil {
        fmt.Fprintln(os.Stderr, "reading quotes:", err)
    }

    fmt.Println(len(quotes))
    for i, quote := range quotes {
        fmt.Println(i, quote)
    }
}

var shakespeare = `To be, or not to be: that is the question$$All the world‘s a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts.$$Romeo, Romeo! wherefore art thou Romeo?$$Now is the winter of our discontent$$Is this a dagger which I see before me, the handle toward my hand?$$Some are born great, some achieve greatness, and some have greatness thrust upon them.$$Cowards die many times before their deaths; the valiant never taste of death but once.$$Full fathom five thy father lies, of his bones are coral made. Those are pearls that were his eyes. Nothing of him that doth fade, but doth suffer a sea-change into something rich and strange.$$A man can die but once.$$How sharper than a serpent’s tooth it is to have a thankless child!` + "\n"

游乐场:https://play.golang.org/p/zMuWMxXJyQ

输出:

10
0 To be, or not to be: that is the question
1 All the world‘s a stage, and all the men and women merely players. They have their exits and their entrances; And one man in his time plays many parts.
2 Romeo, Romeo! wherefore art thou Romeo?
3 Now is the winter of our discontent
4 Is this a dagger which I see before me, the handle toward my hand?
5 Some are born great, some achieve greatness, and some have greatness thrust upon them.
6 Cowards die many times before their deaths; the valiant never taste of death but once.
7 Full fathom five thy father lies, of his bones are coral made. Those are pearls that were his eyes. Nothing of him that doth fade, but doth suffer a sea-change into something rich and strange.
8 A man can die but once.
9 How sharper than a serpent’s tooth it is to have a thankless child!