英文:
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)
}
这意味着:
- 不要定义一个全局函数
visit
(无法使用任何额外的参数调用,比如yourWord
) - 在调用
filepath.Walk
之前,定义一个callback
函数(函数字面量)
详见“Codewalk: First-Class Functions in Go”。
函数字面量和闭包
在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:
- instead of defining a global function
visit
(which cannot be called with any additional parameters, likeyourWord
) - you define a
callback
function (function literal) just before callingfilepath.Walk
with it
See more at "Codewalk: First-Class Functions in Go"
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()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论