Created
April 23, 2015 02:56
-
-
Save me7/45ee299a91741782ebe2 to your computer and use it in GitHub Desktop.
walk directory and read file content to array of strings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"path/filepath" | |
"os" | |
"io/ioutil" | |
"strings" | |
) | |
func walkFn(path string, info os.FileInfo, err error) error { | |
filename := info.Name() | |
if strings.Contains(filename, "csv") { | |
basename := filepath.Base(filename) | |
println(strings.TrimSuffix(basename, filepath.Ext(basename))) | |
content, err := ioutil.ReadFile(filename) | |
if err != nil { | |
return err | |
} | |
lines := strings.Split(string(content), "\r\n") | |
fmt.Printf("%v\n\n",lines[7]) | |
} | |
return nil | |
} | |
func main(){ | |
fmt.Println("start run") | |
err := filepath.Walk(".", walkFn) | |
if err != nil { | |
println(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment