读取一个 golang 文件,并将其中的单词 “search” 替换为大写形式。

huangapple go评论82阅读模式
英文:

Read a golang file and replace word search with uppercase

问题

我正在尝试在目录中的文件中搜索特定的单词,并将所有文件中的每个实例转换为大写。

我想要跟踪找到该单词的文件以及它在这些文件中出现的次数。

到目前为止,我能够读取文件的内容。如何从主函数发送参数以搜索文件并将这些实例替换为大写?

以下是我目前的代码:

func visit(path string, fi os.FileInfo, err error) error {
    if err != nil {
        return err
    }
    
    if fi.IsDir() {
        return nil
    }
    
    matched, err := filepath.Match("*.txt", fi.Name())
    
    if err != nil {
        fmt.Println(err)
        return err
    }
    
    if matched {
        read, err := ioutil.ReadFile(path)
        fmt.Println(read)
        check(err)
        fmt.Println(path)
    }
    
    return nil
}

func main() {
    err := filepath.Walk(".", visit)
}

请注意,这只是读取文件并打印其内容的代码,还没有实现搜索和替换的功能。你需要在visit函数中添加逻辑来搜索特定的单词并将其替换为大写。

英文:

I am trying to search for or a particular words in the files in a directory and and convert each of its instance in all the file in Uppercase.

I want to keep track of the file where the word was found and how many time it appeared in those file.

So far I am able to read the content of the file.
How do send an argument then from the main function to search through the file and replace those instance into upper case?

This is what I have so far:

func visit(path string, fi os.FileInfo, err error) error {

  	if err!=nil{
  	 	return err
  	 }
  	 
  	 if !!fi.IsDir(){
  	 	return nil //
  	 }
  	 
  	  matched, err := filepath.Match("*.txt", fi.Name())
  	  
  	  if err !=nil{
  	  	fmt.Println(err)
  	  	return err
  	  }
  	  
  	  if matched{
  	  read,err:= ioutil.ReadFile(path)
  	  fmt.Println(read)
  	  check(err)
  	  fmt.Println(path) }
  	  
  return nil
} 

func main() {
 
  err := filepath.Walk(".", visit)
}

答案1

得分: 1

你可以使用一个变量作为闭包,被filepath.Walk使用的callback函数字面量所看到。
例如,参考“GOLANG: Walk Directory Tree and Process Files”。

func main() {

    var yourWord := "a word"
    
    callback := func(path string, fi os.FileInfo, err error) error {
            // 使用yourWord做一些操作!
    }
    
    filepath.Walk(".", callback)

}

这意味着:

函数字面量和闭包

在Go中,可以声明匿名函数。
函数字面量是闭包:它们继承了声明它们的函数的作用域
它们可以引用周围函数中定义的变量。这些变量在周围函数和函数字面量之间共享,并且只要可以访问它们,它们就会一直存在。

在你的情况下,callback函数字面量将继承之前定义的变量yourWord


注意:要调用的函数是strings.ToUpper(),而不是strings.toUpper()

英文:

You can use a variable which is seen by a callback function literal, used by filepath.Walk, as a closure.
See for instance "GOLANG: Walk Directory Tree and Process Files".

func main() {

    var yourWord := "a word"
    
    callback := func(path string, fi os.FileInfo, err error) error {
            // do something with yourWord!
    }
    
    filepath.Walk(".", callback)

}

That means:

Function literals and closures

> Anonymous functions can be declared in Go.
Function literals are closures: they inherit the scope of the function in which they are declared.
They may refer to variables defined in a surrounding function. Those variables are then shared between the surrounding function and the function literal, and they survive as long as they are accessible.

In your case, the 'callback' function literal will inherit from the variable yourWord defined before.


Note: the function to call is strings.ToUpper(), not strings.toUpper().

huangapple
  • 本文由 发表于 2014年7月17日 21:14:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/24804625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定